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

当iphone sdk中有多个分区时,如何更改同一分区中不同单元格的大小和颜色,iphone,cocoa-touch,ios,uitableview,Iphone,Cocoa Touch,Ios,Uitableview,我想更改指定区域中不同行(单元格)的大小和颜色,以便任何人都能给我建议如何管理它。。 实际上,我在同一个视图中使用了4个部分,我想在中更改单元格颜色和单元格大小 第4节为此,我在“heightforrowatinexpath”方法中编写了以下代码 谢谢和问候, Priyanka。您不能在表格视图:heightForRowAtIndexPath:方法中更改单元格的颜色。您需要在tableView:cellforrowatinexpath:方法中执行此操作。但是,您应该在此方法中传递自定义高度。所以

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

谢谢和问候,
Priyanka。

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

- (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;
        }
    }
}

假设
label6
label7
单元格的成员

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

为了更改节中特定单元格的高度,请复制并粘贴表视图的“行高度”方法,这是一种委托协议,您已经在代码中完成了。下面是“我的代码”中的一个视图

- (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; } -(CGFloat)tableView:(UITableView*)表视图行高度索引路径:(NSIndexPath*)索引路径 { 开关(indexPath.section){ 案例0: if(indexPath.row==2) { 返回25; } 打破 案例1: if(indexPath.row==2) { 返回25; } 打破 } 返回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; } -(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath { 静态NSString*CellIdentifier=@“Cell”; UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 如果(单元格==nil){ cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:CellIdentifier]自动释放]; } if(indexPath.section==0) { cell.textlab.text=[arr objectAtIndex:indexath.row]; if(indexPath.row==2) { //改变细胞颜色 cell.backgroundColor=[UIColor grayColor]; } } else if(indexPath.section==1) { if(indexPath.row==2) { //改变细胞颜色 cell.backgroundColor=[UIColor blueColor]; } cell.textlab.text=[arr1 objectAtIndex:indexath.row]; } 返回单元; }
希望这能有所帮助……

这段代码到底有什么问题?你的问题不清楚…只有我代码的其他部分工作不正常。那么你对此有什么建议吗…实际上,我已经在CellForRowatineXpath()中初始化了这两个标签(label6和label7)根据章节和else代码,我只需更改其大小和颜色。单元格或标签的颜色如果您使用自定义单元格,那么在这种情况下,您可以使用您创建的标签来代替textLabel。很高兴我能提供帮助。奇怪的是,被接受的答案似乎已经改变了。仍然有一些lill问题,实际上我已经在开关盒上面初始化了label6,如“label6=[[UILabel alloc]initWithFrame:CGRectMake(0.00120,44)];”在开关盒中,如“label6.frame=CGRectMake(0,0320,44);”所以根据“label6=[[UILabel alloc]initWithFrame:CGRectMake(0.00120,44)];“标签将被设置。但我想要320,而不是120,所以任何解决方案。。 - (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; }