更改UITableView for iPhone SDK中备用行的颜色

在我的应用程序中,我想要在iPad中显示多列tableView。

所以,我使用了一个来自MultiColumn TableView的cocoa控件

它对我来说工作正常,但是我想要以交替的颜色显示行。

为此,我无法find我更改代码的位置。

帮我解决这个问题。

请尝试使用它,它会正常工作..

- (void)tableView: (UITableView*)tableView willDisplayCell: (UITableViewCell*)cell forRowAtIndexPath: (NSIndexPath*)indexPath { if(indexPath.row % 2 == 0) cell.backgroundColor = [UIColor redColor]; else cell.backgroundColor = [UIColor whiteColor]; } 

使用cellForRowAtIndexPath的indexPath来获得所需的结果。

  if( [indexPath row] % 2){ cell.backgroundColor=[UIColor whiteColor]; } else{ cell.backgroundColor=[UIColor purpleColor]; } 

你将在实现UITableViewDelegate的类中拥有这个委托方法

试试这个代码::

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { ....... if (indexPath.row % 2 == 0) { cell.backgroundColor = [UIColor lightGrayColor]; } else { cell.backgroundColor = [UIColor darkGrayColor]; } ....... return cell; } 

希望,它会帮助你。

谢谢。

首先你需要把每一行的模块化。

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { ....... if (indexPath.row % 2 == 0) { cell.backgroundColor = [UIColor lightGrayColor]; } else { cell.backgroundColor = [UIColor darkGrayColor]; } .... return cell; } 

任何人试图迅速做到这一点是我的代码。 小而精美。

这个技巧甚至可以处理静态单元格,我不使用cellForRowAtIndexPath方法。

对不起,小答案。 希望你了解一个代表的数据源。

  override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { if (indexPath.row % 2) != 0{ cell.backgroundColor = UIColor .blackColor() }else{ cell.backgroundColor = UIColor .lightGrayColor() } } 

它会给你的结果像…

使用Swift为ios中的每个替代单元着色

谢谢

希望这有助于。

像这样尝试

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; } if(indexPath.row%2==0) cell.contentView. backgroundColor=[UIColor redColor]; else cell.contentView.backgroundColor=[UIColor greenColor]; return cell; } 

在代码中searchcellForRowAtIndexPath。 该方法负责在你的tableview中创build单元格。 欲了解更多信息: UITableViewCell与自定义单元格中的备用背景颜色

在你的cellForRow方法中,你应该检查indexPath.row是否可以被2分配第一个颜色整除,否则分配第二个颜色并返回结果单元格

 func colorForIndex(index: Int) -> UIColor { let itemCount = stnRepos.count - 1 let color = (CGFloat(index) / CGFloat(itemCount)) * 0.6 return UIColor(red: 0.80, green: color, blue: 0.0, alpha: 1.0) } func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { if (indexPath.row % 2 == 0) { cell.backgroundColor = colorForIndex(indexPath.row) } else { cell.backgroundColor = UIColor.whiteColor()() } } 

你可以运行一个循环,将索引path.row加2,然后在该循​​环的主体中,你可以指定颜色。

对于Swift 3 +和不透明度降低:

 func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { if(indexPath.row % 2 == 0) { cell.backgroundColor = UIColor.red.withAlphaComponent(0.05) } else { cell.backgroundColor = UIColor.white } }