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



我想更改指定部分中不同行(单元格)的大小和颜色,因此任何人都可以给我如何管理它的建议。实际上我在同一视图上使用 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;
            }
        }

}

谢谢和问候,普里扬卡。

现在从上面的问题中,据我所知,您想更改您在代码中制作的某些部分的高度和单元格颜色 好吧,请尝试以下代码

为了更改部分中特定单元格的高度,复制+粘贴表视图的行高度方法,这是您已经在代码中完成的委托协议,这是我的代码中的视图

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{    switch (indexPath.section) {        案例 0:            if(indexPath.row ==2)            {                返回 25;            }            破;        案例1:            if(indexPath.row ==2)            {                返回 25;            }            破;}    返回表视图.行高度;  }

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    静态 NSString *CellIdentifier = @"Cell";    UITableViewCell *cell = [tableView dequeueReuseableCellWithIdentifier:CellIdentifier];    如果(单元格 == 无){        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];    }    if(indexPath.section==0)    {        cell.textLabel.text = [arr objectAtIndex:indexPath.row];        if(indexPath.row ==2)        {            更改单元格颜色            cell.backgroundColor = [UIColor grayColor];        }    }    else if(indexPath.section ==1)    {        if(indexPath.row ==2)        {            更改单元格颜色            cell.backgroundColor = [UIColor blueColor];        }        cell.textLabel.text = [arr1 objectAtIndex:indexPath.row];    }    返回细胞;}

希望这有帮助.....

不能更改 tableView:heightForRowAtIndexPath: 方法中单元格的颜色。您需要在tableView:cellForRowAtIndexPath:方法中执行此操作。但是,您应该在此方法中传递自定义高度。所以它会变成,

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section) {
    case 1:
        return 100;
    case 3:
        if (indexPath.row == 1) {
            return 100;
        }
    default:
        return 44;
    }
}

您可以在

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
    ...
    if ( indexPath.section == 3 ) {
        switch ( indexPath.row ) {
        case 0:
            cell.label6.frame=CGRectMake(0, 0, 320, 44);
            cell.label6.backgroundColor=[UIColor yellowColor];
            break;
        case 1:
            cell.label6.frame=CGRectMake(0.0, 0,120,100); 
            cell.label6.backgroundColor=[UIColor purpleColor];
            cell.label7.frame=CGRectMake(122, 0,200,100); 
            cell.label7.backgroundColor=[UIColor purpleColor];
            break;
        default:
            cell.label6.frame=CGRectMake(0.0, 0,120,40); 
            cell.label7.frame=CGRectMake(122, 0,200,40);
            break;
        }
    }
}

假设label6label7cell的成员。

最新更新