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