Iphone 更改UITableviewCell中的哪个按钮图像?

Iphone 更改UITableviewCell中的哪个按钮图像?,iphone,Iphone,我在UITableviewCell中有一个带图像的按钮可供选择。我添加了按钮操作作为toggleButton方法。如果我触摸特定单元格中的按钮, 相应tablecell按钮的图像已更改。但当我滚动tableview时,更改的图像位于另一个单元格中。我如何避免它?请查看我的代码…您能告诉我必须执行的操作吗?我不想使用did select方法,在该方法中我必须执行其他操作。请提供帮助 - (UITableViewCell *)tableView:(UITableView *)tableView

我在UITableviewCell中有一个带图像的按钮可供选择。我添加了按钮操作作为toggleButton方法。如果我触摸特定单元格中的按钮, 相应tablecell按钮的图像已更改。但当我滚动tableview时,更改的图像位于另一个单元格中。我如何避免它?请查看我的代码…您能告诉我必须执行的操作吗?我不想使用did select方法,在该方法中我必须执行其他操作。请提供帮助

   - (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];

    onButton = [UIButton buttonWithType:UIButtonTypeCustom];
    onButton.tag = 1;       

    onButtonView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 50)];
    onButtonView.tag = 2;
    onButtonView.image = [UIImage imageNamed:@"NotSelected.png"];
    [onButton setBackgroundImage:[onButtonView.image stretchableImageWithLeftCapWidth:0.0 topCapHeight:0.0] forState:UIControlStateNormal];
    [cell addSubview:onButton];
    [onButton addTarget:self action:@selector(toggleButton:) forControlEvents: UIControlEventTouchUpInside];
    [onButtonView release];
 } 


return cell;

  }

当您在
-cellforrowatinexpath
方法中重用单元格时,必须重置其所有属性,否则该单元格可能具有您以前为另一个indexPath设置的属性。

因此,您必须将单元格状态保存在某个位置,并在每次调用方法时在
-cellforrowatinexpath
中为您的onButton设置一个映像。

-cellforrowatinexpath
方法中重用单元格时,必须重置其所有属性,否则该单元格可能具有您以前为另一个indexPath设置的属性。

因此,您必须将单元格状态保存在某个位置,并在
-cellforrowatinexpath
中为每次调用方法时的onButton设置一个图像。

您会遇到此问题,因为您正在使用显示的单元格。这是创建单元格的正确方法,否则会占用大量内存

首先,从if案例中删除以下内容。把它放在下面:

onButton = [UIButton buttonWithType:UIButtonTypeCustom];
onButton.tag = 1;               

onButtonView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 50)];
onButtonView.tag = 2;
onButtonView.image = [UIImage imageNamed:@"NotSelected.png"];
[onButton setBackgroundImage:[onButtonView.image stretchableImageWithLeftCapWidth:0.0 topCapHeight:0.0] forState:UIControlStateNormal];
[cell addSubview:onButton];
[onButton addTarget:self action:@selector(toggleButton:) forControlEvents: UIControlEventTouchUpInside];
[onButtonView release];
您所做的是尝试通过“dequeueReusableCellWithIdentifier”获取单元格。这意味着,如果该单元格尚不存在,(
if(cell==nil)
)它将创建一个单元格。 您仅在创建单元格时设置按钮图像。如果在If大小写之后设置,即使单元格不是nil,也会始终将图像设置为“未选定”


首先,它可能会解决您的问题。

您之所以会遇到此问题,是因为您正在使用显示的单元格。这是创建单元格的正确方法,否则会占用大量内存

首先,从if案例中删除以下内容。把它放在下面:

onButton = [UIButton buttonWithType:UIButtonTypeCustom];
onButton.tag = 1;               

onButtonView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 50)];
onButtonView.tag = 2;
onButtonView.image = [UIImage imageNamed:@"NotSelected.png"];
[onButton setBackgroundImage:[onButtonView.image stretchableImageWithLeftCapWidth:0.0 topCapHeight:0.0] forState:UIControlStateNormal];
[cell addSubview:onButton];
[onButton addTarget:self action:@selector(toggleButton:) forControlEvents: UIControlEventTouchUpInside];
[onButtonView release];
您所做的是尝试通过“dequeueReusableCellWithIdentifier”获取单元格。这意味着,如果该单元格尚不存在,(
if(cell==nil)
)它将创建一个单元格。 您仅在创建单元格时设置按钮图像。如果在If大小写之后设置,即使单元格不是nil,也会始终将图像设置为“未选定”


首先,它可能会解决您的问题。

您的
tableView:cellforrowatinexpath:
方法将返回以前使用过的不再使用的单元格。您需要确保,如果重复使用单元格,则会重置图像。您需要这样做:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell) {
    // Reusing cell; make sure it has correct background
    UIImageView *onBut = (UIButton *)[cell viewWithTag:1];
    onBut.image = [UIImage imageNamed:@"NotSelected.png"];
    // etc.
}
else {
    // Create cell
    // ...
}

请注意,如果所选单元格被卷回视图,则需要将图像设置为“所选”图像。

您的
表格视图:cellForRowAtIndexPath:
方法将返回以前使用过的不再使用的单元格。您需要确保,如果重复使用单元格,则会重置图像。您需要这样做:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell) {
    // Reusing cell; make sure it has correct background
    UIImageView *onBut = (UIButton *)[cell viewWithTag:1];
    onBut.image = [UIImage imageNamed:@"NotSelected.png"];
    // etc.
}
else {
    // Create cell
    // ...
}

请注意,如果您选择的单元格被卷回视图,则需要将图像设置为“选定”图像。

如果您限制了表格大小,这是使用预初始化单元格创建NSArray的最简单方法。 对于十几个单元来说,内存使用不是必需的

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [myCells objectAtIndex:indexPath.row];
}

若您的表大小有限,那个么使用预先初始化的单元格创建NSArray是最简单的方法。 对于十几个单元来说,内存使用不是必需的

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [myCells objectAtIndex:indexPath.row];
}

真的,同一个问题的问题太多了)仅仅因为你们已经看过这个问题很多次并不意味着提问者已经看过了。这些侮辱真的没有必要。同意,对不起,我的回答并非有意冒犯你。我不是以英语为母语的人,可能感觉不到一些语言细节。对不起,阿甘·拉迪米尔,请你举个例子好吗?真的,同一个问题的问题太多了)仅仅因为你们已经看过这个问题很多次并不意味着提问者已经看过了。这些侮辱真的没有必要。同意,对不起,我的回答并非有意冒犯你。我不是以英语为母语的人,可能感觉不到一些语言细节。对不起,请你举个例子好吗?我相信这会在每次单元格滚动到视图时添加一个UIButton的新实例。这可能不可见,但由于单元格内有许多视图,因此会降低滚动性能。我按你说的做了……问题是?例如,当我触摸第一个单元格的按钮时,图像会发生变化。当我滚动tableview时,第一个单元格按钮的图像位于最后一个单元格按钮(已更改)。。。。有什么帮助吗?按钮操作系统的图像已更改…但未保存在tableviewcell中。。但是当我滚动tableview时,它消失了我猜你的意思是该单元格被重置了?在我的头顶上。。。您可以尝试创建一个NSMutableArray,并将每个按钮更改添加到其中。加载单元格时,检查按钮是否存在(例如,使用indexath.row),如果不存在。。。显示未选中的图像,如果是。。。显示选中的图像。任何有更好想法的人,欢迎回答:)我相信这将在每次单元格滚动到视图时添加一个新的UIButton实例。这可能不可见,但由于单元格内有许多视图,因此会降低滚动性能。我按你说的做了……问题是?例如,当我触摸第一个单元格的按钮时,图像会发生变化。当我滚动tableview时,第一个单元格按钮的图像位于最后一个单元格按钮(已更改)。。。。有什么帮助吗?按钮操作系统的图像已更改…但未保存在tableviewcell中。。但是当我滚动tableview时,它消失了我猜你的意思是该单元格被重置了?在我的头顶上。。。您可以尝试创建一个NSMutableArray,并将每个按钮更改添加到其中。加载单元格时,检查按钮是否存在(例如,使用indexath.row),如果不存在。。。显示未选中的图像,如果是。。。显示选中的图像。有没有人