UITableView使用方法(三) - 交替颜色单元格的实现

来源:互联网 发布:lol徐老师淘宝店网址 编辑:程序博客网 时间:2024/05/16 00:31


效果如下:
























主要是在tableView的cellForRowAtIndexPath中实现,实现代码如下:

- (UITableViewCell *) tableView:(UITableView *)tableView          cellForRowAtIndexPath:(NSIndexPath *)indexPath{           NSString *tag=@"tag";    CGSize size;    CGFloat actualSize;        UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:tag];        if (cell==nil ) {        cell=[[[ UITableViewCell alloc ]                initWithStyle:UITableViewCellStyleSubtitle               reuseIdentifier:tag] autorelease];            }            NSUInteger row=[indexPath row];         //设置文本    cell.textLabel.text = [listData objectAtIndex :row];        //---------------------------------------------------------    //设置颜色交替    if( (row%2) ==0)        cell.backgroundColor = [UIColor whiteColor];    else        cell.backgroundColor = [UIColor lightGrayColor];    //---------------------------------------------------------        cell.detailTextLabel.text = [listData objectAtIndex :row];        return cell;}

比较简单,就是改变不同行的背景就行了。


运行环境:

MacOS 10.7.4 + XCode 4.3.3 + iOS 5.1


原创粉丝点击