---
title: "Tableview详解"
author: "zcw_java"
date: "2013-08-22T15:47:18Z"
tags: []
categories: ["移动开发"]
slug: "tableview"
draft: false
lastmod: "2013-08-22T16:33:19Z"
id: 1929329
url: "https://zcw-java.iteye.com/blog/1929329"
---

一，为tableview中cell，修改其样式

```c++

[tableview deselectrowatindexpath:indexpath animated:yes];
    UITableViewCell *celling = [tableview cellforrowatindexpath:indexpath];
    for (int i = 0; i < [m_mutarrinitmodeldatalist count]; i++)
    {
        NSIndexPath *indexPath1 = [nsindexpath indexpathforrow:i insection:0]; 
        UITableViewCell *cell = (UITableViewCell *)[tableview cellforrowatindexpath:indexpath1];
        if([cell.textlabel.text isequaltostring:celling.textlabel.text])
        {
            if([cell accessorytype] == UITableViewCellAccessoryNone)
            {
                cell.accessoryType = UITableViewCellAccessoryCheckmark;
            }
            else
            {
                cell.accessoryType = UITableViewCellAccessoryNone;
            }
        }
        else
        {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
    }

```


```c++

// 通过int获取NSIndexPath
NSIndexPath *indexPath1 = [nsindexpath indexpathforrow:i insection:0]; 
// 获取用户选中indexPath
NSIndexPath *indexPath1 = [tableview indexpathforselectedrow];

```


//设置Section的数量
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{ 
 return TitleData; 
} 
//设置每个section显示的Title 
- (NSString *)tableView:(UITableView *)tableViewtitleForHeaderInSection:(NSInteger)section{ 
 return @"Andy-清风"; 
} 
 
//指定有多少个分区(Section)，默认为1 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
 return 2; 
} 
 
//指定每个分区中有多少行，默认为1 
- (NSInteger)tableView:(UITableView *)tableViewnumberOfRowsInSection:(NSInteger)section{ 
} 
 
//设置每行调用的cell 
-(UITableViewCell *)tableView:(UITableView *)tableViewcellForRowAtIndexPath:(NSIndexPath *)indexPath { 
static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier"; 
   
    UITableViewCell *cell = ; 
    if (cell == nil) {   
        cell =  initWithStyle:UITableViewCellStyleDefault 
                                       reuseIdentifier:SimpleTableIdentifier] autorelease]; 
 } 
 cell.imageView.image=image;//未选cell时的图片
 cell.imageView.highlightedImage=highlightImage;//选中cell后的图片
 cell.text=@”Andy-清风”; 
 return cell; 
} 
//设置让UITableView行缩进
-(NSInteger)tableView:(UITableView *)tableViewindentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{ 
 NSUInteger row = ; 
 return row; 
} 
//设置cell每行间隔的高度
- (CGFloat)tableView:(UITableView *)tableViewheightForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    return 40; 
} 
//返回当前所选cell 
NSIndexPath *ip = ; 
; 
 
//设置UITableView的style 
; 
//设置选中Cell的响应事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{ 
 
 ;//选中后的反显颜色即刻消失
} 
 
//设置选中的行所执行的动作
 
-(NSIndexPath *)tableView:(UITableView *)tableViewwillSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSUInteger row = ; 
     return indexPath; 
} 
//设置划动cell是否出现del按钮，可供删除数据里进行处理
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath*)indexPath { 
} 
//设置删除时编辑状态
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
｝
 
//右侧添加一个索引表
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{ 
} 
 
 
（2）  其他
 
//选中cell时的颜色，在官方文档有如下可以选择
 
typedef enum { 
    UITableViewCellSelectionStyleNone, 
    UITableViewCellSelectionStyleBlue, 
    UITableViewCellSelectionStyleGray 
} UITableViewCellSelectionStyle 
 
  
 
//cell右边按钮格式
 
typedef enum { 
    UITableViewCellAccessoryNone,                   //don't show any accessory view 
    UITableViewCellAccessoryDisclosureIndicator,    //regular chevron. doesn't track 
    UITableViewCellAccessoryDetailDisclosureButton, //blue button w/ chevron. tracks 
    UITableViewCellAccessoryCheckmark               //checkmark. doesn't track 
} UITableViewCellAccessoryType 
 
  
 
//是否加换行线
 
typedef enum { 
    UITableViewCellSeparatorStyleNone, 
    UITableViewCellSeparatorStyleSingleLine 
} UITableViewCellSeparatorStyle 
 
  
 
//改变换行线颜色
 
tableView.separatorColor= ; 
 
 
 
4.    UITableViewCell
表中的每一行都代表一个UITableViewCell。可以使用图像、文本还有辅助的图标等来自定义你自己的UITableViewCell。你可以自定义你自己的cell如下模型或者像appstore那样的。
 
UITableViewCell为每个Cell提供了三个可以选择的属性，如下：
l textLabel：填写文本
l detailTextLable：稍微详细的副标题
l imageView：用来显示你cell的图片，可以通过UIImage来加载。
最后给出一个官方的demo给大家学习下，多实践，不懂的就问下，下节课讲些UITableView应用中实际会出现的问题，比如自定义啊，重用单元格，单元格的数据排序等问题。欢迎大家拍砖。
附上代码：http://www.2cto.com/uploadfile/2011/1130/20111130025401502.zip