iphone - 当我们在 iphone sdk 中有多个部分时,如何更改同一部分中不同单元格的大小和颜色

标签 iphone cocoa-touch ios uitableview

我想更改指定部分中不同行(单元格)的大小和颜色,所以任何人都可以给我建议如何管理它。 实际上我在同一 View 上使用 4 个部分,我想更改单元格颜色和单元格大小 第 4 节,我在“heightForRowAtIndexPath”方法中编写了以下代码

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0)
    {
        return 44;
    }
    else if(indexPath.section==2)
    {
        return 44;

    }
    else if(indexPath.section==1)
    {
        return 100;
    }
    else 
    {
        if (indexPath.row==0) 
        {
            label6.frame=CGRectMake(0, 0, 320, 44);
            label6.backgroundColor=[UIColor yellowColor];
            return 44;
        }

        else if(indexPath.row==1)
            {
                return 100;
                label6.frame=CGRectMake(0.0, 0,120,100); 
                label6.backgroundColor=[UIColor purpleColor];
                label7 .frame=CGRectMake(122, 0,200,100); 
                label7.backgroundColor=[UIColor purpleColor];

            }
            else
            {
                label6.frame=CGRectMake(0.0, 0,120,40); 
                label7 .frame=CGRectMake(122, 0,200,40); 

                return 44;
            }

        }


}

谢谢和问候, 普里扬卡。

最佳答案

现在,据我了解,从上述问题来看,您想更改代码中某些部分的高度和单元格颜色,请尝试以下代码

为了更改部分中特定单元格的高度,请复制并粘贴 TableView 的行方法的高度,这是您已经在代码中完成的委托(delegate)协议(protocol),这是我的代码中的 View

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    switch (indexPath.section) {
        case 0:
            if(indexPath.row ==2)
            {
                return 25;
            }
            break;

        case 1:
            if(indexPath.row ==2)
            {
                return 25;
            }
            break;


    }

    return tableView.rowHeight;   
}
现在,在下一部分中,我所做的是更改某些行的单元格颜色,我希望代码是不言自明的。选择索引路径方法中行的单元格并添加此代码。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    if(indexPath.section==0)
    {
        cell.textLabel.text = [arr objectAtIndex:indexPath.row];

        if(indexPath.row ==2)
        {
            //changing cell color
            cell.backgroundColor = [UIColor grayColor];
        }

    }
    else if(indexPath.section ==1)
    {
        if(indexPath.row ==2)
        {
            //changing cell color
            cell.backgroundColor = [UIColor blueColor];
        }
        cell.textLabel.text = [arr1 objectAtIndex:indexPath.row];
    }

    return cell;
}

希望这有帮助......

关于iphone - 当我们在 iphone sdk 中有多个部分时,如何更改同一部分中不同单元格的大小和颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6080145/

相关文章:

ios - FMDB 由于 sqlite3_step 和 while (重试)而无限循环

ios - 如何在 JTCalender 中设置日期限制?

iphone - 涉及新实体和一对多关系的核心数据迁移

iphone - 使用 Amazon IOS SDK 上传 S3

cocoa-touch - CALayer 和离屏渲染

iphone - iOS 中图像的 CITemperatureAndTint

ios - 如何在iphone 的NSUserDefaults 中动态生成多个 key

iphone - iOS iPhone 应用程序中的数据核心 Unresolved 问题

iOS 7 淡入淡出状态栏文本?

ios - 如何在 Swift 中使用 PromiseKit 和 Firebase?