【问题标题】:UiButton text wont stay changed in Tableview cellUiButton 文本不会在 Tableview 单元格中保持更改
【发布时间】:2014-10-02 08:16:19
【问题描述】:

我已经搜索了整个网络,但似乎无法找到解决这个简单问题的方法。当工作“喜欢”时,我有一个按钮的表格视图。按下按钮时,它会将单词更改为“Unlike”。我让它工作但是当我向下滚动表格时,我看到其他按钮也变为“不喜欢”,有时会与“喜欢”重叠。当我向上滚动时,我选择的原始按钮会变回正常状态。我知道单元格是可重用的,这就是为什么我为我的数据源使用可变数组但它仍然不起作用的原因。请帮忙!

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"SimpleTableCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }
    UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [myButton setTitle:@"Like" forState:UIControlStateNormal];
    myButton addTarget:self action:@selector(tapped:) forControlEvents:UIControlEventTouchUpInside];
    myButton.frame = CGRectMake(14.0, 10.0, 125.0, 25.0);
    myButton.tag =indexPath.row;
    [cell.contentView addSubview:myButton];
    cell.textLabel.text = [recipes objectAtIndex:indexPath.row];  
 return cell;
}

动作方法:

-(void)tapped:(id)sender {
    UIButton *senderButton = (UIButton *)sender;

    UITableViewCell *parentCell = [[sender superview]superview];

    NSIndexPath *indexPathLiked = [table indexPathForCell:parentCell];

    [array replaceObjectAtIndex:senderButton.tag withObject:[NSNumber numberWithInt:1]];

    [sender setTitle:@"Unlike" forState:UIControlStateNormal];

}

【问题讨论】:

    标签: ios uitableview ios7 uibutton


    【解决方案1】:

    如果表格调用了 cellForRowAtIndexPath,您将始终创建一个新按钮。当你得到一个重复使用的单元格时,按钮仍然存在,你在那里放一个新的。

    将您的方法更改为:

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
        UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [myButton setTitle:@"Like" forState:UIControlStateNormal];
        myButton addTarget:self action:@selector(tapped:) forControlEvents:UIControlEventTouchUpInside];
        myButton.frame = CGRectMake(14.0, 10.0, 125.0, 25.0);
        myButton.tag =indexPath.row;
        [cell.contentView addSubview:myButton];
    }
    else {
       // todo: change the button title to "like" or "unliked" value
    }
    cell.textLabel.text = [recipes objectAtIndex:indexPath.row];  
    

    附:这没有意义,你不使用那个,你为什么这样做?

    UITableViewCell *parentCell = [[sender superview]superview];
    NSIndexPath *indexPathLiked = [table indexPathForCell:parentCell];
    

    如果你只有一个部分,你可以在不使用 superview 的情况下获取单元格:

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:sender.tag inSection:0]
    UITableViewCell *parentCell = [table cellForRowAtIndexPath:indexPath];
    

    【讨论】:

    • 运气不好 :(。未选中的单元格仍然受到影响
    • 可能是你尝试继承你的 UITableViewCell。然后你对单元有更多的控制权。例如,您可以覆盖方法“prepareForReuse”。我认为要解决问题,您必须调试整个过程以找出问题所在...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多