iPhone上的TableView的问题重用单元 [英] Problem reuse cell of TableView on iPhone

查看:66
本文介绍了iPhone上的TableView的问题重用单元的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表格视图中有一个单元格列表,其中只有最后一个单元格具有公开指示符(它会触发操作).当我向下滚动我的单元格列表时,一切正常,但是如果我向后滚动,奇怪的是,显示指示器也出现在其他单元格中.我真的不知道问题出在哪里,有什么帮助吗?

谢谢, 丹尼尔(Daniele)

这是所使用的代码部分:

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;
}

解决方案

这不是bug:这是一个功能;),因为正在重用单元格.

如果表视图包含200个单元格,并且iphone能够同时显示5个单元格,则您只有5-6个UITableViewCell实例.如果您向下滚动一个单元格,则会看到显示"按钮,如果向下滚动,该单元格将被重用,结果,该显示"按钮仍然存在.

解决您的问题:

方法1:不仅在最后一个单元格上设置显示按钮.您还应该在其他单元格中将其删除/取消设置.

方法2:似乎最后一个单元格是语义上的另一种单元格.因此,请选择一个重用标识符,例如:@"MyLastCell用于最后一个单元格,@"MyCell"用于所有其他单元格.结果,您的表视图将仅重用相同类型的单元格(在您的情况下:带有/不带有显示按钮)

:方法2的一些样本伪代码; 缩短方法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
    }   
}

修改2 :方法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 ;

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] initWithStyle: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 ;

这篇关于iPhone上的TableView的问题重用单元的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆