简体   繁体   English

tableView中的奇怪错误

[英]Strange bug in tableView

I have a UITableView, I can add and delete cell to this table. 我有一个UITableView,可以向此表添加和删除单元格。 I also have two buttons. 我也有两个按钮。 1 button adds "1" to the cell's text, Which is 1 so basically it counts when pressing the + button and subs when pressing the - button. 1按钮在单元格的文本中添加“ 1”,即为1,因此基本上在按下+按钮时将计数,而在按下-按钮时将进行计数。 My problem is with the very last cell. 我的问题是最后一个单元格。 If i add 5 cells, its the 5th cell that has the problem. 如果我添加5个单元格,则是有问题的第5个单元格。 If i add 200 cells its the 200th cell, etc. The problem is when i press the - button, all the other cells keep turning blue when pressed, and this button stops turning blue. 如果我将200个单元格与其第200个单元格相加,等等。问题是当我按-按钮时,所有其他单元格在按时保持蓝色,而该按钮停止变为蓝色。 It stays white when i press it when the cells text is 0. I want it to keep turning blue like all the other cells when pressed. 当单元格文本为0时,当我按它时它保持白色。我希望它像其他所有单元格一样保持蓝色。 Here is my code: 这是我的代码:

- (IBAction)subtractLabelText:(id)sender
{
    cell = (UITableViewCell*)[sender superview];    

    if ( [[cell.textLabel text] intValue] == 0){ 
        [newBtn setEnabled:NO];
    }
    else{
        cell.textLabel.text = [NSString stringWithFormat:@"%d",[cell.textLabel.text    
        intValue] -1];
        [newBtn setEnabled:YES];
    }
}

This method is hooked up to the sub "-" button. 此方法已连接到子“-”按钮。 Also, when i press the button when the text is = 0, the button is there but when i press it, it selects the cell and the table cell turns blue as if i selected that! 另外,当我在文本= 0时按按钮时,按钮在那里,但是当我按按钮时,它选择了单元格,表格单元格变成了蓝色,就像我选择了那样! Please help! 请帮忙! Thanks everybody! 谢谢大家!

cellForRow: cellForRow:

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

    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
        reuseIdentifier:CellIdentifier];
    } 
    cell.imageView.image = [imageArray objectAtIndex:indexPath.row];    
    cell.textLabel.text = [cells objectAtIndex:indexPath.row];

    newBtn = [[UIButton alloc]init];
    newBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    [newBtn setFrame:CGRectMake(260,20,55,35)];
    [newBtn addTarget:self action:@selector(subtractLabelText:) 
        forControlEvents:UIControlEventTouchUpInside];
    [newBtn setTitle:@"-" forState:UIControlStateNormal];
    [newBtn setEnabled:YES];
    [cell addSubview:newBtn];

    subBtn = [[UIButton alloc]init];
    subBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    [subBtn setFrame:CGRectMake(200,20,55,35)];
    [subBtn addTarget:self action:@selector(addLabelText:) 
        forControlEvents:UIControlEventTouchUpInside];
    [subBtn setTitle:@"+" forState:UIControlStateNormal];
    [subBtn setEnabled:YES];
    [cell addSubview:subBtn];
    return cell;
}

At a guess, I'd say it has to do with how the UITableViewCell s are being reused, in that the UI doesn't 'know' about the subview buttons on those cells. 大概我想说这与UITableViewCell的重用有关,因为UI并不“知道”这些单元格上的子视图按钮。

Try commenting out the code in -cellForRowAtIndexPath that dequeues the reusable cells (so in effect you're always creating a new UITableViewCell regardless of whether cell==nil or not). 尝试注释掉-cellForRowAtIndexPath可重用单元格出队的代码(因此,实际上,无论cell==nil是否,您总是在创建一个新的UITableViewCell )。

Edit: You're referencing newBtn in -subtractLabelText , but this is an ivar that doesn't necessarily refer to the button that sent the message. 编辑:你引用newBtn-subtractLabelText ,但是这是伊娃并不一定是指发送消息的按钮。 Try [sender setEnabled:NO] instead. 请尝试使用[sender setEnabled:NO]

Based on your cellForRowAtIndexPath method, you are creating and adding new buttons to each cell regardless of whether they are being reused or 'brand new'. 基于cellForRowAtIndexPath方法,您正在创建新按钮并将其添加到每个单元格,无论它们是被重用还是“全新”。

Your code is doing something like this: 您的代码正在执行以下操作:

  1. Check if there is a cell to be dequeued/reused 检查是否有一个单元要出队/重用
  2. If yes, grab a reference to it. 如果是,请获取对其的引用。 If no, create a new one. 如果否,请创建一个新的。
  3. Add buttons XYZ to the cell (your mistake is here) 将XYZ按钮添加到单元格(您的错误在这里)
  4. Return the cell for the tableview to use 返回表格视图使用的单元格

Imagine that in step 2, if you a reusing an existing cell, this cell already has the buttons XYZ added to it so you are essentially adding two extra button to it when you didn't need to. 想象一下,在第2步中,如果您重复使用现有单元格,则此单元格中已经添加了XYZ按钮,因此实际上您不需要时在其中添加了两个额外的按钮。

You should move the code that sets up the cell buttons to inside the if (cell == nil) loop so the buttons are only created and added to the content view if you are initialising a new cell object. 您应该将用于设置单元格按钮的代码移动到if (cell == nil)循环内,以便仅在初始化新的单元格对象时创建按钮并将其添加到内容视图中。

Also make sure you look at the additional comments below which are not directly related to your question but may be relevant nonetheless. 另外,请确保您查看下面的其他注释,这些注释与您的问题没有直接关系,但可能仍然相关。

================================================================== ================================================== ================

[EDIT - ANSWER ABOVE, BUT THIS IS ALSO RELEVANT SO I LEFT IT HERE] [编辑-上面的答案,但是这也很重要,所以我在这里留下了]

This will not solve your problem, but you should be adding your buttons to the cell's contentview instead of directly to the cell, ie: 这不会解决您的问题,但是您应该将按钮添加到单元格的contentview而不是直接添加到单元格中,即:

[cell.contentView addSubView:buttonABC];

Once that's done, you will need to call superview twice to get the cell reference in your subtract/add method: 完成后,您将需要调用superview两次,以在减/加方法中获取单元格引用:

cell = (UITableViewCell*)[[sender superview] superview];    

From Apple's documentation on UITableViewCell 摘自Apple在UITableViewCell上的文档

The content view of a UITableViewCell object is the default superview for content displayed by the cell. UITableViewCell对象的内容视图是单元格显示的内容的默认超级视图。 If you want to customize cells by simply adding additional views, you should add them to the content view so they will be positioned appropriately as the cell transitions into and out of editing mode. 如果要通过简单地添加其他视图来自定义单元格,则应将其添加到内容视图中,以便在单元格进入和退出编辑模式时它们的位置适当。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM