您的位置:首页 > 产品设计 > UI/UE

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  UITableView section