[ios]设置表格单元格交替背景 【转】

通常,表格单元格的定制如下列代码所示。这个例子来自于NavigationBased app项目模板,我在单元格的标签上加入文本"Row"。

// Customize the appearance of table view cells.

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

     }

        // Configure the cell.

        cell.textLabel.text = @"Row";

     return cell;

}

你可以在这段代码里设置单元格的背景,但你会得到一些莫名其妙的效果。iOS在创建单元格时做了一些额外的工作并且为显示进行了优化,比如不管你愿意不愿意,无论单元格有多么复杂,它都会在单元格周围加上蓝色的选区。

在WWDC的视频上,我发现设置单元格交替颜色的最佳时机并不是在创建单元格的时候。还有一个方法,恰巧会在单元格即将绘制的前一秒被调用。

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

        cell.backgroundColor = (indexPath.row%2)?[UIColor lightGrayColor]:[UIColor grayColor];

 }

注意C条件表达式导致了颜色交替显示。%是取模运算符。?:是简化的if...else语句。当单元格“即将”显示时,该方法被调用。

对于每一节,单元格的行号从0开始。但我们必须计算单元格在整个表格总行数中的序号。这只能靠我们自己来完成。因为协议方法中已经有返回节数和给定节的行数的方法,这个计算显得十分简单。代码如下:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

     return 4;

}

// Customize the number of rows in the table view.

- (NSInteger)tableView:(UITableView *)tableView

  numberOfRowsInSection:(NSInteger)section {

     return 3;

}  

- (NSInteger)realRowNumberForIndexPath:(NSIndexPath *)indexPath

  inTableView:(UITableView *)tableView {

        NSInteger retInt = 0;

        if (!indexPath.section)        {

               return indexPath.row;

        }

        for (int i=0; i<indexPath.section;i++)     {

               retInt += [tableView numberOfRowsInSection:i];

        }

        return retInt + indexPath.row;

}  

- (void)tableView:(UITableView *)tableView

  willDisplayCell:(UITableViewCell *)cell

  forRowAtIndexPath:(NSIndexPath *)indexPath {

        NSInteger realRow = [self realRowNumberForIndexPath:indexPath inTableView:tableView];

        cell.backgroundColor = (realRow%2)?[UIColor lightGrayColor]:[UIColor grayColor];

}  

- (NSString *)tableView:(UITableView *)tableView

  titleForHeaderInSection:(NSInteger)section {

        return [NSString stringWithFormat:@&quot;Section %d&quot;, section];

}  

// Customize the appearance of table view cells.

- (UITableViewCell *)tableView:(UITableView *)tableView

  cellForRowAtIndexPath:(NSIndexPath *)indexPath {

       static NSString *CellIdentifier = @&quot;Cell&quot;;

       UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

     if (cell == nil) {

         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

     }

        // Configure the cell.

        cell.textLabel.text = @&quot;Row&quot;;

     return cell;

}

原文地址:https://www.cnblogs.com/jinjiantong/p/3000420.html