I am creating a s开发者_C百科imple shopping list application, and with it I am displaying an array of items.
How can I display quantity of each item after its name?
You can try using a custom UITableViewCell
which will enable you to display the cell with contents the way you want it to. This will help you.
Edit the UITableViewCellStyle in cellForRowAtIndexpath as UITableViewCellStyleValue1 or UITableViewCellStyleValue2 and can access two labels in a row as shown in below images....
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell.
return cell;
}
In all these cell styles, the larger of the text labels is accessed via the textLabel property and the smaller via the detailTextLabel property.
for more click here
You could us a formatted string for this. Instead of setting the labels value to just the name, do it like this:
cell.textLabel.text = [NSString stringWithFormat:@"%@ (%i)", yourNameVariable, quantity]
Edit_: If alignment is not a problem, you can go this way, otherwise use @7KV7 solution
I would recommend using custom table view cells. You can check this tutorial out, for help
You can write a method to create a customized UITableViewCell
-(UITableViewCell*)customCell
{
static NSString *CustomCellIdentifier = @"CustomCell";
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CustomCellIdentifier];
CGRect labelFrame = CGRectMake(x, y, width, height);
UILabel *itemNameLabel = [[[UILabel alloc] initWithFrame:labelFrame] autorelease];
itemNameLabel.textAlignment = UITextAlignmentLeft;
itemNameLabel.backgroundColor = [UIColor clearColor];
CGRect anotherLabelFrame = CGRectMake(x, y, width, height);
UILabel *quantityLabel = [[[UILabel alloc] initWithFrame:anotherLabelFrame] autorelease];
quantityLabel.textAlignment = UITextAlignmentLeft;
quantityLabel.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:itemNameLabel];
[cell.contentView addSubview:quantityLabel];
return cell;
}
OR
You can Subclass the UITableViewCell and use it.
精彩评论