2013-06-18 87 views
0

在ios6中, [UITableView dequeueReusableCellWithIdentifier:forIndexPath:]总是返回一个单元格。 那么,如果我想在我的单元格中添加一些按钮处理程序,并且避免每次重用单元格时添加目标。使用[tableView 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)。

任何建议表示赞赏,

雨果

回答

0

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

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

......你会发现,你只能收到playInputClicked:每分头一次,而不是100次。

+0

抱歉超长时间延迟(仅2y ...)。不知道我怎么错过通知...... thx! –

相关问题