开发者

Problem reuse cell of TableView on iPhone

I have a list of cells in a table view where only the last cell has a disclosure indicator (it triggers an action). When I scroll down my list of cells everything works fine, but if I scroll back up oddly the disclosure indicator appears in other cells too. I can't really figure out where's the problem, any help?

Thanks, Daniele

This is the part of code that is use:

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {

    cell = [[[UITableViewCell alloc] ini开发者_JAVA技巧tWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];     
}
cell.textLabel.text = [myArray objectAtIndex:indexPath.row];
if(([myArray count]-1) == indexPath.row) {
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}


that is not a bug: it is a feature ;) because the cells are being reused.

If your tableview contains 200 cells and your iphone is able to show 5 cells at the same time then you only have 5-6 instances of UITableViewCell. If you scroll down a cell gets the disclosure-button and if you scroll back the cell is being reused and as a result the disclosure-button is still there.

to solve your problem:

approach 1: not only set the disclosure-button on the last cell. you should also remove/unset it in other cells.

approach 2: it seems that the last cell is another type of cell in semantic. So choose a reuse-identifier for example: @"MyLastCell for the last cell and @"MyCell" for all other cells. As a result your tableview will only reuse cells of the same type (in your case: with/without disclosure-button)

edit 1: some sample-pseudo-code for approach 2 ; edit 3: shorter solution for approach 2

    static NSString *CellIdentifier = @"Cell";
static NSString *LastCellIdentifier = @"LastCell";

bool isLastRow = (indexPath.row == numRows-1);
NSString *usedCellIdentifier = isLastRow ? LastCellIdentifier : CellIdentifier;

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: usedCellIdentifier];

if(!cell)
{
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:usedCellIdentifier] autorelease];
    if(isLastCell)
    {
        //do disclosure-stuff here. Or add a UIButton here
        cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton
    }   
}

edit 2: sample code for approach 1

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {

    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];     
}

cell.accessoryType = (idexPath.row == numRows-1) ? 
    UITableViewCellAccessoryDetailDisclosureButton 
    : UITableViewCellAccessoryNone ;


As thomas said, you can use his code and you can add this:

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

for the last cell (inside the if where you dequeue your last cell) and this:

cell.accessoryType = UITableViewCellAccessoryNone;

for other cells (inside the if where you dequeue other cells).

I haven't tried this code but it should work... ;)


try this

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {

    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];     
}
cell.textLabel.text = [myArray objectAtIndex:indexPath.row];
if(([myArray count]-1) == indexPath.row) {
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
else{

    cell.accessoryType = UITableViewCellAccessoryNone;
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜