Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Row index from indexPath

I often call a web service when a view loads and save an NSMutableArray with the contents of the call. Then in cellForRowAtIndexPath I retrieve the rows from this array. I use what feels like a hack to get the row index from the indexPath, however:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    <snip cell alloc stuff>

    // Configure the cell.
    unsigned int *indexes = (unsigned int*)calloc( [indexPath length], sizeof( unsigned int ) );
    [indexPath getIndexes:indexes];
    Tasks *task = [_tasksArray objectAtIndex:indexes[1]];   
    free( indexes );

    cell.textLabel.text = [task getname];
    cell.detailTextLabel.text = [NSString stringWithFormat:@"Started: %@", [task getstarted] ? [[task getstarted] description] : @"Not Yet"];

    return cell;
}

Is there a better way to do this?

like image 819
Jeremy Mullin Avatar asked Mar 23 '11 15:03

Jeremy Mullin


People also ask

What does indexPath row return?

So to start with the indexPath. row will be 0. Then it will be 1, then 2, then 3 and so on. You do this so that you can get the correct string from the array each time.

How do you get the indexPath row when a button in a cell is tapped?

add an 'indexPath` property to the custom table cell. initialize it in cellForRowAtIndexPath. move the tap handler from the view controller to the cell implementation. use the delegation pattern to notify the view controller about the tap event, passing the index path.

What is indexPath row?

Index paths describe an item's position inside a table view or collection view, storing both its section and its position inside that section. For example, the first row in a table would have section 0, row 0, whereas the eighth row in the fourth section would have section 3, row 7.

What is difference between indexPath row and indexPath item?

Thus the two are functionally identical. In terms of programming style – and perhaps the definition chain – you would be better using "row" in the context of tables, and "item" in the context of collections. The core interface of NSIndexPath is defined in NSIndexPath. h.


1 Answers

Try to access the row part of the indexPath

indexPath.row

Here's a link to Apple's documentation for NSIndexPath.

like image 89
Nick Weaver Avatar answered Sep 28 '22 19:09

Nick Weaver