我在故事板中放置了一個tableview,並用一個「0」作爲標記的按鈕填充單元格。我使用數組填充tableview。讓我們說我的數組中有20個元素,這使得它在桌面視圖上有20個單元格。這是我用來給每個單元格上的按鈕添加標籤的代碼。表視圖中的按鈕標記重置?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView == _tblCart) {
static NSString *cellIdentifier = @"CartCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
UIButton *btn = (UIButton *)[cell viewWithTag:0];
[btn setTag:indexPath.row];
[btn addTarget:self action:@selector(logTag:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
return nil;
}
我以爲我的代碼可以正常工作,但實際上有些問題。在單元格0-5上,按鈕標籤是正確的。在下面的單元格中,標籤將重置爲0.我做錯了什麼?
你的做法是不正確的,不需要設置按鈕標記爲細胞指數 – 2014-10-22 10:26:56