![](/img/trans.png)
[英]New or Existing cell when using dequeueReusableCellWithIdentifier:forIndexPath:?
[英]add action handler to button in cell in ios6 using [tableView dequeueReusableCellWithIdentifier:forIndexPath:]
在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)。
任何建議表示贊賞,
胡格斯
只需再次添加即可。 該按鈕會自動消除重復的目標/動作對。 如果您這樣做:
for (int i = 0; i < 100; ++i) {
[cell.playButton addTarget:self action:@selector(playInputClicked:) forControlEvents:UIControlEventTouchUpInside];
}
…您會發現您只收到playInputClicked:
每次點擊一次,而不是100次。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.