2013-06-20 42 views
0

我在我的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 ... 

我想保持標籤號碼是唯一的(不重複的原因)是這樣的,我知道哪個按鈕對應於哪一行。我怎樣才能做到這一點?

回答

2

你按照我的回答之前,我wnat地告訴你,下面的代碼對內存管理是不好的,因爲它將爲每行UITableView創建新的單元格,因此請小心。

但它是更好地使用,當UITableView具有有限行(約50-100而定)然後下面的代碼是在您的案件有幫助的,使用它,如果它是適合你的。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    NSString *CellIdentifier = [NSString stringWithFormat:@"S%1dR%1d",indexPath.section,indexPath.row]; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if(cell == nil) 
    { 
     cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 

     /// Put your code here 
    } 

    return cell; 
} 
+1

+1,以獲取有關內存管理的註釋。當內存管理不好時,很多用戶都會告訴你。 – Popeye

+0

@ Popeye-感謝它對初學者很好的理解。 :) – iPatel

+1

是的,很多用戶都不瞭解可重複使用的單元格內容,他們認爲他們可以創建儘可能多的單元格,而不會造成問題。 – Popeye

0

按鈕標籤,不要設置您創建的單元格,如果塊中,因爲細胞會被重用當細胞被重用你是不是在方案中更新標籤。

編輯: 對於第5個細胞,當正在執行if塊和正在創建新細胞 。第6個單元以後單元正在被重新使用,因此計數不會增加。

打電話給你

button.tag = (indexPath.row)+100; 

下方的if (cell == nil)塊,像這樣:

if (cell == nil) 
    { 
     //... 

    } 
    else 
    { 
     ///... 
     button = (UIButton*)[[cell subviews] lastObject]; //this assumes your button is the last view in the subview array (double check) 
    } 
    button.tag = (indexPath.row)+100; 
+0

我只是做了,它並沒有區別。仍然計數從100-104,然後超過 –

+0

當單元格重用 - 按鈕零?如果是這樣,你需要進入單元格並手動獲取按鈕子視圖,並更新標記 – foggzilla

0

你需要做的水木清華這樣的:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    self.rNewsCell = [self.tabelView dequeueReusableCellWithIdentifier:allNewsCellIdentifier forIndexPath:indexPath]; 

    if (self.news.count != 0) 
    { 
     if(indexPath.row <[self.news count]) 
     { 
      RNewsModel *news = (self.news)[indexPath.row]; 
      self.rNewsCell.newsImage.contentMode = UIViewContentModeScaleAspectFill; 
      self.rNewsCell.newsName.text = news.newsNameModel; 
      self.rNewsCell.newsData.text = news.newsDataModel; 
      self.rNewsCell.newsWatches.text = news.newsWatchesModel; 
      self.rNewsCell.newsAddress.text = news.newsAddressModel; 
      self.rNewsCell.newsTime.text = news.newsTimeModel; 
      self.rNewsCell.playFrame.image = [UIImage imageNamed: news.newsPlayImage]; 
      self.rNewsCell.playButton.tag = indexPath.row; 
      self.rNewsCell.showOnMap.tag = indexPath.row; 

      self.rNewsCell.rightUtilityButtons = [self rightButtons]; 
      self.rNewsCell.delegate = self; 
     } 
    } 
    return self.rNewsCell; 
} 

,之後趕上按鈕操作:

- (IBAction)showNewsOnTheMap:(id)sender 
{ 
    UIButton *button=(UIButton *) sender; 
    self.showNewsOnMap = [NSIndexPath indexPathForRow:button.tag inSection:0]; 
    RNewsModel *news = (self.news)[self.showNewsOnMap.row]; 
    NSLog(@"didSelect NEWSOnMap = %@",news.newsIDModel); 
    [self performSegueWithIdentifier:SegueShowNewsOnTheMap sender:self]; 
}