On Jun 25, 2009, at 11:46 AM, Alexander Spohr wrote:

Am 25.06.2009 um 10:54 schrieb WT:

On Jun 25, 2009, at 10:26 AM, Alexander Spohr wrote:

Why didn’t you put the cell into the controllers nib?
Make an outlet in your controller to reach the cell and just return it when you need it?

Because -tableView:cellForRowAtIndexPath: needs to return a different cell object for each visible row. Your code above returns always the same object, the one table view cell archived in the nib, which means that changing the data in one cell would change the data in all visible cells, and all visible cells would have the same data all the time.

Of course, you are right. I forgot that the OP wanted something like an accessory which needs to be repetitive.

        atze

Even with a custom cell with no extra controls, you'd need to return a different cell object for each visible row. Try this and see what happens:

- (NSInteger) tableView: (UITableView*) table_view
  numberOfRowsInSection: (NSInteger) section
{
    return 10;
}

- (UITableViewCell*) tableView: (UITableView*) table_view
cellForRowAtIndexPath: (NSIndexPath*) index_path
{
    NSInteger row = [index_path row];

    static NSString* cellID = @"cellID";

    UITableViewCell* cell = nil;

    cell = [table_view dequeueReusableCellWithIdentifier: cellID];

    if (cell == nil)
    {
        cell = customCell;
    }

    if (row % 2 == 0)
    {
        cell.contentView.backgroundColor = [UIColor blueColor];
    }
    else
    {
        cell.contentView.backgroundColor = [UIColor redColor];
    }

    return cell;
}

You will NOT see a table with alternating colors for its cells. I had thought that you'd see all cells with the same color, but in fact I was wrong about that. What you do see is that only ONE cell has its background colored. And it makes perfect sense, since cells are views and the ONE custom cell is repeatedly added as a subview to the table view "content" area for each row, but only the last one "sticks" because, again, you only have one cell object.

Wagner_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to