zoukankan      html  css  js  c++  java
  • [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;

    }

  • 相关阅读:
    PyQt(Python+Qt)学习随笔:使用pyqtConfigure建立信号和槽的连接
    PyQt(Python+Qt)学习随笔:调用disconnect进行信号连接断开时的信号签名与断开参数的匹配要求
    PyQt(Python+Qt)学习随笔:什么是信号绑定(Unbound and Bound Signals)?
    PyQt(Python+Qt)学习随笔:信号签名(signature of the signal)是什么?
    第六章、信号和槽进阶--自定义信号及其他信号、槽的高级特性
    第15.19节 PyQt(Python+Qt)入门学习:自定义信号与槽连接
    第五章、信号和槽的实战应用--一个计算器的实现
    第四章 、PyQt中的信号(signal)和槽(slot)机制以及Designer中的使用
    第三章 、使用Qt Designer进行GUI设计
    织梦通过 phpmyadmin 导出的数据,再次导入的时候报错
  • 原文地址:https://www.cnblogs.com/jinjiantong/p/3000420.html
Copyright © 2011-2022 走看看