문제

In my app, I want to show how many cells there are in a table view by adding a number in number order. For example, I want the first cell in the table view to have a 1 next to it and the second cell to have a 2 next to it.

Thanks!

도움이 되었습니까?

해결책

You can do something like this in your tableView:cellForRowAtIndexPath: method

cell.textLabel.text = [NSString stringWithFormat:@"%i", indexPath.row+1];

Edit:

Then make sure you do the following:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10; <<<<====specify here the number of rows to be displayed.
}

Addendum:

If you already have things stored in your labels and would like to store still the numbers, try the do the following:

In your storyboard, click on the cell and then change the style to "Subtitle".

Then back to your table view controller, add the following line to the aforementioned method:

cell.detailTextLabel.text = [NSString stringWithFormat:@"%i", indexPath.row+1];

So now you have both labels, one in which you would like to store your strings and the sub labels where you have the number of rows as discussed.

Adding screenshot:

다른 팁

Try the following in your tableView:cellForRowAtIndexPath: implementation:

cell.textLabel.text = [NSString stringWithFormat:@"%@ %i",[tableData objectAtIndex:indexPath.row], ((indexPath.row)+1)];

You could do something like this where you can keep "textLabel" for whatever you want to put there and use "detailTextLabel" to show the number of the row on the right while using UITableViewCellStyleValue1.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
    [[cell textLabel] setText:@"Your Label"];
    [[cell detailTextLabel] setText:[NSString stringWithFormat:@"%d", [indexPath row]+1]]; //Note the +1 because the first row will have an indexpath 0
    return cell;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top