uitableview自定义按钮标记问题

时间:2013-06-20 14:34:08

标签: iphone ios uitableview

我正在我的uitable中创建一个自定义按钮,并为该按钮添加一个标签号,如下面的代码所示。

// 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 ...

我想保持标签号唯一(而不是重复)的原因是我知道哪个按钮对应于哪一行。我怎样才能做到这一点?

3 个答案:

答案 0 :(得分:2)

在你按照我的回答之前,我告诉你,以下代码对内存管理不利,因为它会为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 :(得分:0)

不要在创建单元格的if块中设置按钮标记,因为单元格将被重用,并且在重用单元格时不会更新场景中的标记。

  

编辑:   对于前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;

答案 2 :(得分:0)

你需要做那样的事情:

ambari-server start --auto-fix-database

然后抓住按钮动作:

- (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;
}