UITableView中设置section内行数以及配置cell的方法
来源:互联网 发布:大连育知同创学费 编辑:程序博客网 时间:2024/06/05 09:08
文中代码粘贴复制自苹果UITableView的Recipes范例:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 4;}- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { NSString *title = nil; // Return a title or nil as appropriate for the section. switch (section) { case TYPE_SECTION: title = @"Category"; break; case INGREDIENTS_SECTION: title = @"Ingredients"; break; default: break; } return title;;}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { NSInteger rows = 0; /* The number of rows depends on the section. In the case of ingredients, if editing, add a row in editing mode to present an "Add Ingredient" cell. */ switch (section) { case TYPE_SECTION: case INSTRUCTIONS_SECTION: rows = 1; break; case INGREDIENTS_SECTION: rows = [recipe.ingredients count]; if (self.editing) { rows++; } break;default: break; } return rows;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = nil; // For the Ingredients section, if necessary create a new cell and configure it with an additional label for the amount. Give the cell a different identifier from that used for cells in other sections so that it can be dequeued separately. if (indexPath.section == INGREDIENTS_SECTION) {NSUInteger ingredientCount = [recipe.ingredients count]; NSInteger row = indexPath.row; if (indexPath.row < ingredientCount) { // If the row is within the range of the number of ingredients for the current recipe, then configure the cell to show the ingredient name and amount.static NSString *IngredientsCellIdentifier = @"IngredientsCell";cell = [tableView dequeueReusableCellWithIdentifier:IngredientsCellIdentifier];if (cell == nil) { // Create a cell to display an ingredient.cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:IngredientsCellIdentifier] autorelease];cell.accessoryType = UITableViewCellAccessoryNone;} Ingredient *ingredient = [ingredients objectAtIndex:row]; cell.textLabel.text = ingredient.name;cell.detailTextLabel.text = ingredient.amount; } else { // If the row is outside the range, it's the row that was added to allow insertion (see tableView:numberOfRowsInSection:) so give it an appropriate label.static NSString *AddIngredientCellIdentifier = @"AddIngredientCell";cell = [tableView dequeueReusableCellWithIdentifier:AddIngredientCellIdentifier];if (cell == nil) { // Create a cell to display "Add Ingredient".cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:AddIngredientCellIdentifier] autorelease];cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;} cell.textLabel.text = @"Add Ingredient"; } } else { // If necessary create a new cell and configure it appropriately for the section. Give the cell a different identifier from that used for cells in the Ingredients section so that it can be dequeued separately. static NSString *MyIdentifier = @"GenericCell"; cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease]; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; } NSString *text = nil; switch (indexPath.section) { case TYPE_SECTION: // type -- should be selectable -> checkbox text = [recipe.type valueForKey:@"name"]; cell.accessoryType = UITableViewCellAccessoryNone; cell.editingAccessoryType = UITableViewCellAccessoryDisclosureIndicator; break; case INSTRUCTIONS_SECTION: // instructions text = @"Instructions"; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; cell.editingAccessoryType = UITableViewCellAccessoryNone; break; default: break; } cell.textLabel.text = text; } return cell;}
- UITableView中设置section内行数以及配置cell的方法
- UITableView中刷新单行cell与单个section段的方法
- UITableView中刷新单行cell与单个section段的方法
- UITableView中刷新单行cell与单个section段的方法
- 设置UITableView Section、cell背景颜色
- UITableview中隐藏静态section的方法
- UITableView刷新指定的section和cell
- (原创)iOS中cell高度自适应方法UITableView设置
- ios 刷新 UITableview 的某个 Section 或者 cell 的两个方法
- 详解iOS开发中UITableview cell 顶部空白的多种设置方法
- 几种设置UITableView的cell动态高度的方法
- iOS UITableView中关于cell里的按钮被点击时如何确定是哪一个section
- iOS UITableView中关于cell里的按钮被点击时如何确定是哪一个section
- iOS UITableView中关于cell里的按钮被点击时如何确定是哪一个section
- UITableView的cell一些设置
- UITableView刷新指定section、cell
- UITableView Cell的属性方法
- iOS UITableview 刷新指定的某一个section或cell
- X264和JM的区别
- 【黑马程序员】身份证验证
- c++例题 构造函数(一)
- 挺好的话
- [Android] Ubuntu中下载android源码
- UITableView中设置section内行数以及配置cell的方法
- C++中宏的使用技巧
- 获得js文件引用的参数
- Generic Programming Techniques
- html table复杂表头、列固定
- 手机毒霸去广告功能分析之一:总体分析
- IE下载文件时,下载文件为何出现“无法复制文件:无法读源文件或磁盘
- Using SQL*Loader to create an external table
- android 电视盒下XBMC源码功能的简单分析