0

在 ios6 中, [UITableView dequeueReusableCellWithIdentifier:forIndexPath:] 总是返回一个单元格。那么如果我想在我的单元格上添加一些按钮处理程序,并避免在每次重用单元格时添加目标。

现在,我使用标签来记住单元格是否已经连接:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"MyCell";
    MyCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    if(!cell.tag){
        cell.tag = 1;
        [cell.playButton addTarget:self action:@selector(playInputClicked:) forControlEvents:UIControlEventTouchUpInside];
    }
    return cell;
}

任何更好的解决方案(不使用 registerClass 或 registerNib)。

任何建议表示赞赏,

雨果

4

1 回答 1

0

只需再次添加它。该按钮会自动消除重复的目标/动作对。如果你这样做:

for (int i = 0; i < 100; ++i) {
    [cell.playButton addTarget:self action:@selector(playInputClicked:) forControlEvents:UIControlEventTouchUpInside];
}

…您会发现playInputClicked:每次点击只会收到一次,而不是 100 次。

于 2013-06-18T23:09:35.343 回答