开发者

UITableView with a single custom Cell? (iPhone)

开发者 https://www.devze.com 2022-12-21 02:24 出处:网络
I have run into a bit of a problem. Usually when dealing with UITableView I will build a special method, configureCell, that builds the cells as the tableView needs them. So cellForRowAtIndexPath quer

I have run into a bit of a problem. Usually when dealing with UITableView I will build a special method, configureCell, that builds the cells as the tableView needs them. So cellForRowAtIndexPath queries the configureCell to get a new cell if none is found in the cache.

In the part of cellForRowAtIndexPath that gets run every time a user scrolls:

- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *CellIdentifier = @"Cell";

        UITableViewCell *cell = [theTableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {       
            cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
            [self configureCell:cell atIndexPath:indexPath];
        }
//this part is always executed!
}

I set the label values etc. from my model. This works fine and this, I believe, is how it should be to work properly and be the least strain on the CPU. From what I can read in the TableView Guide.

My problem is now that the first Cell in my tableView is different than the others. I has a special icon and other things that separates it from the other cells.

In my configure cell I then tried 开发者_如何学JAVAasking:

if (indexPath.row == 0) {

    //do special setup!

}

This is a bad idea as the tableView lazy loads the cells and therefore the first cell that is off-screen, when being scrolled on-screen, will also get indexPath.row = 0 so now I have a "special cell" for every 7 cells. How do I get around this one?

A second problem that also originates from the above: I have a custom UIView placed in all the cells accessoryView. When the user taps an "Edit" button, all the cells accessoryViews should change to an icon indicating that we are in "Edit mode". Here, again, this only happens for the cells on screen, when scrolling "old" cells are either fetched from cache or new cells are build that doesn't know we are in edit mode.

When You tap a Cell, there is never any doubt about the indexPath.row of the cell tapped, this index you need to pair with your model array to figure out what the user tapped. But it seems different rules apply when the TableView is manipulating the cells (pulling them on-screen, off-screen, deleting, adding).

I guess, I am asking; to follow best practice and not fight the SDK, what should I do to obtain the desired functionality and at which stage in the Cell life-cycle should I apply this logic?

Hope someone can guide me in the right direction:) thank You in advance:)


I think the problem is not that the first off-screen cell is at indexPath.row = 0. The problem is that you are using the same CellIdentifier for regular and custom cells. So when the TableView goes to dequeue a cached cell, it is sometimes grabbing your custom cell that has the icon in it.

Since you gave it the same CellIdentifier as your regular cells, it doesn't know that they are not the same.

Something like this should fix it:

static NSString *CellIdentifier;
if([indexPath row] == 0){
    CellIdentifier = @"CustomCell";
} else {
    CellIdentifier = @"Cell";
}

I'm not sure about your edit button problem, but it could be related.

0

精彩评论

暂无评论...
验证码 换一张
取 消