我在我的uitable中創建了一個自定義按鈕,併爲該按鈕提供了一個如以下代碼所示的標籤編號。uitabelview自定義按鈕標籤問題
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//NSLog(@"Inside cellForRowAtIndexPath");
static NSString *CellIdentifier = @"Cell";
// Try to retrieve from the table view a now-unused cell with the given identifier.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// If no cell is available, create a new one using the given identifier.
if (cell == nil)
{
// Use the default cell style.
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:@selector(selectPicOnBtnTouch:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:@"btn1" forState:UIControlStateNormal];
button.frame = CGRectMake(6.0f, 0.0f, 97.0f, 110.0f);
button.tag = (indexPath.row)+100;
[cell addSubview:button];
[button setImage:[UIImage imageNamed:@"sample_pic.png"] forState:UIControlStateNormal];
}
else
{
//NSLog(@"went here 2");
button = (UIButton *) [cell viewWithTag:((indexPath.row)+100)];
}
//Get an arrow next to the name
cell.accessoryType = UITableViewCellAccessoryNone;
return cell;
}
- (IBAction) selectPicOnBtnTouch:(id)sender
{
button = (UIButton *)sender;
NSLog(@"[button tag]: %d ...", [button tag]);
}
如果我有10行的表,當我觸摸按鍵在1-5行我得到了我的日誌
[button tag]: 100 ...
[button tag]: 101 ...
[button tag]: 102 ...
[button tag]: 103 ...
[button tag]: 104 ...
以下這很好。問題是,當我觸摸排6-10我期待105 - 109標籤,但我得到的回覆是相同的標籤號重新開始
[button tag]: 100 ...
[button tag]: 101 ...
[button tag]: 102 ...
[button tag]: 103 ...
[button tag]: 104 ...
我想保持標籤號碼是唯一的(不重複的原因)是這樣的,我知道哪個按鈕對應於哪一行。我怎樣才能做到這一點?
+1,以獲取有關內存管理的註釋。當內存管理不好時,很多用戶都會告訴你。 – Popeye
@ Popeye-感謝它對初學者很好的理解。 :) – iPatel
是的,很多用戶都不瞭解可重複使用的單元格內容,他們認爲他們可以創建儘可能多的單元格,而不會造成問題。 – Popeye