views:

676

answers:

2

Hello,

I am using following method in my application:

  • (UITableViewCell *)tableView:(UITableView *)tblView cellForRowAtIndexPath:(NSIndexPath *)indexPath

In this method by using indexPath.row I can get the row number of each row.

But I want to actually access the cell at that row and do some formatting on that cell only.

Please guide me.

Regards, Pratik

+1  A: 

You can access a visible cell using UITableView -cellForRowAtIndexPath method. From UITableView reference:

- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath

Return Value
An object representing a cell of the table or nil if the cell is not visible or indexPath is out of range.

However (IMO) cellForRowAtIndexPath is the best place for a cell setup.

Vladimir
+1  A: 

Maybe there is a misunderstanding. The method you are quoting is supposed to tell the tableView which row to display at that indexPath. So, whenever the tableView asks for this method, it does so because there is no row at this indexPath yet.

The template code will generate a new cell or dequeue one:

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

     static NSString *CellIdentifier = @"Cell";

     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
     if (cell == nil) {
              cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
     }

You can then access cell to configure the content of each cell or add content to the contentView.

If you manipulate the formatting of a cell on a per row basis (e.g. the color of the cell.textLabel) you should do this in the following delegate method:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
Felix