UITableView的使用
2016-03-04 16:16
369 查看
调用数据源的下面方法得知一共有多少组数据
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
调用数据源的下面方法得知每一组有多少行数据
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
调用数据源的下面方法得知每一行显示什么内容
- (UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
Cell的重用原理
还有一个非常重要的问题:
有时候需要自定义UITableViewCell(用一个子类继承UITableViewCell),而且每一行用的不一定是同一种UITableViewCell,所以一个UITableView可能拥有不同类型的UITableViewCell,对象池中也会有很多不同类型的UITableViewCell,那么UITableView在重用UITableViewCell时可能会得到错误类型的UITableViewCell
解决方案:UITableViewCell有个NSString *reuseIdentifier属性,可以在初始化UITableViewCell的时候传入一个特定的字符串标识来设置reuseIdentifier(一般用UITableViewCell的类名)。当UITableView要求dataSource返回UITableViewCell时,先通过一个字符串标识到对象池中查找对应类型的UITableViewCell对象,如果有,就重用,如果没有,就传入这个字符串标识来初始化一个UITableViewCell对象
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
调用数据源的下面方法得知每一组有多少行数据
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
调用数据源的下面方法得知每一行显示什么内容
- (UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
// 一共有多少组数据 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; // 每一组有多少行数据 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; // 每一行显示什么内容 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; - 具体代码如下**
@implementation MJViewController - (void)viewDidLoad { [super viewDidLoad]; // 设置数据源 self.tableView.dataSource = self; } #pragma mark - 数据源方法 /** * 一共有多少组数据 */ - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 3; } /** * 第section组有多少行 */ - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (section == 0) { // 德系品牌 return 3; } else if (section == 1) { // 日韩品牌 return 4; } else { // 欧系品牌 return 2; } } /** * 每一行显示怎样的内容(cell) */ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil]; if (indexPath.section == 0) { // 德系品牌(第0组) if (indexPath.row == 0) { // 第0组的第0行 cell.textLabel.text = @"奥迪"; } else if (indexPath.row == 1) { // 第0组的第1行 cell.textLabel.text = @"宝马"; } else if (indexPath.row == 2) { cell.textLabel.text = @"奔驰"; } } else if (indexPath.section == 1) { // 日韩品牌(第1组) if (indexPath.row == 0) { // 第1组的第0行 cell.textLabel.text = @"本田"; } else if (indexPath.row == 1) { // 第1组的第1行 cell.textLabel.text = @"丰田"; } else if (indexPath.row == 2) { cell.textLabel.text = @"铃木"; } else if (indexPath.row == 3) { cell.textLabel.text = @"三菱"; } } else if (indexPath.section == 2) { // 欧系品牌(第2组) if (indexPath.row == 0) { // 第2组的第0行 cell.textLabel.text = @"兰博基尼"; } else if (indexPath.row == 1) { // 第2组的第1行 cell.textLabel.text = @"劳斯莱斯"; } } return cell; } /** * 第section组显示怎样的头部标题 */ - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { if (section == 0) { return @"德系品牌"; } else if (section == 1) { return @"日韩品牌"; } else { return @"欧系品牌"; } } /** * 第section组显示怎样的尾部标题 */ - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section { if (section == 0) { return @"世界一流品牌"; } else if(section == 1) { return @"牛逼哄哄"; } else { return @"价格昂贵"; } } @end
Cell的重用原理
还有一个非常重要的问题:
有时候需要自定义UITableViewCell(用一个子类继承UITableViewCell),而且每一行用的不一定是同一种UITableViewCell,所以一个UITableView可能拥有不同类型的UITableViewCell,对象池中也会有很多不同类型的UITableViewCell,那么UITableView在重用UITableViewCell时可能会得到错误类型的UITableViewCell
解决方案:UITableViewCell有个NSString *reuseIdentifier属性,可以在初始化UITableViewCell的时候传入一个特定的字符串标识来设置reuseIdentifier(一般用UITableViewCell的类名)。当UITableView要求dataSource返回UITableViewCell时,先通过一个字符串标识到对象池中查找对应类型的UITableViewCell对象,如果有,就重用,如果没有,就传入这个字符串标识来初始化一个UITableViewCell对象
相关文章推荐
- request请求对象实例
- 解决UITableViewCell左侧分割线有空白的问题
- float和Float的问题,错误原因:cannot be translated into a null value
- Android——TV真机调试apk(android:sharedUserId="android.uid.system")转载
- 98.Which statement is true regarding the COALESCE function?
- NSMutableDictionary改良,默认有key和value
- question_023-JAVA之Collection和Collections
- question_022-JAVA之Map之面试题
- UIScrollView 能够缩放的设置
- iOS UIkit性能优化
- Arduino自定义通信协议解析
- question_021-JAVA之Map之Hashtable???
- Request的getParameter和getAttribute方法的区别
- 设置UIStatusBar的字体颜色
- Android UI 之一步步教你自定义控件(自定义属性、合理设计onMeasure、合理设计onDraw等)
- 使用devenv/MSBuild在命令行编译sln或csproj
- question_020-JAVA之Map之HashMap嵌套HashMap
- 用 UIBezierPath 和 CAShapeLayer 画各种图形
- 泛型算法——quick sort
- 2016.03.04,英语,《Vocabulary Builder》Unit 04