0

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

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

4

3 回答 3

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;
}
于 2013-06-20T14:38:44.690 回答
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;
于 2013-06-20T14:38:10.570 回答
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];
}
于 2017-08-28T10:59:09.023 回答