UITableView使用方法(三) - 交替颜色单元格的实现
效果如下:
主要是在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