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

use*_*099 12 objective-c uitableview ios

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

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

它适用于我,但我想以交替颜色显示行.

为此,我无法找到我为其更改代码的位置.

帮我解决这个问题.

Dha*_*ngh 44

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

- (void)tableView: (UITableView*)tableView willDisplayCell: (UITableViewCell*)cell forRowAtIndexPath: (NSIndexPath*)indexPath
{

        if(indexPath.row % 2 == 0)
              cell.backgroundColor = [UIColor redColor];
        else
              cell.backgroundColor = [UIColor whiteColor];
}
Run Code Online (Sandbox Code Playgroud)


nsg*_*ver 9

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

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

您将在实现该类的类中使用此委托方法 UITableViewDelegate


MS.*_*MS. 6

试试这个代码::

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    .......

    if (indexPath.row % 2 == 0) {
       cell.backgroundColor = [UIColor lightGrayColor];
    }
    else
    {
        cell.backgroundColor = [UIColor darkGrayColor];
    }
    .......

    return cell;
}
Run Code Online (Sandbox Code Playgroud)

希望,它会帮助你.

谢谢.


Mad*_*y B 5

首先,您需要对每行进行模块化.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    .......

    if (indexPath.row % 2 == 0) {
       cell.backgroundColor = [UIColor lightGrayColor];
    }
    else
    {
        cell.backgroundColor = [UIColor darkGrayColor];
    }
    ....


    return cell;
}
Run Code Online (Sandbox Code Playgroud)