UITableView中设置section内行数以及配置cell的方法
2013-03-28 14:37
393 查看
文中代码粘贴复制自苹果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;
}
- (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;
}
相关文章推荐
- iOS UITableView:section的Header以及Footer不悬浮停留在屏幕的方法(非在scrollView代理实现的方法)
- iOS小技巧-UITableView去除底部多余的分割线方法以及设置分割线风格和颜色
- 几种设置UITableView的cell动态高度的方法
- Swift - UIView,UItableView,Cell设置边框方法
- UITableView的section设置了headerView导致下滑的时候headerView跟着滚动的解决方法
- 【iOS】UITableview cell 顶部空白的n种设置方法
- 详解iOS开发中UITableview cell 顶部空白的多种设置方法
- 快速设置UITableView不同section对应于不同种类的cell
- (原创)iOS中cell高度自适应方法UITableView设置
- Swift - UIView,UItableView,Cell设置边框方法
- UITableView性能优化 以及动态计算cell高度 新方法
- 总结了几种设置UITableView的cell动态高度的方法
- iOS设置UITableView的背景图片,以及不显示多余的空Cell
- 几种设置UITableView的cell动态高度的方法
- 详解iOS开发中UITableview cell 顶部空白的多种设置方法
- Tomcat 服务器配置及远程调试和不同web项目session共享方法以及设置TOMCAT的内存
- ios UITableview 刷新某一个cell 或 section
- iOS之设置tableView背景图片/让UITableView自动滑动(定位)到某一行cell
- iOS UITableView中关于cell里的按钮被点击时如何确定是哪一个section
- 解决UITableView中Cell重用机制导致内容出错的方法总结