【问题标题】:iOS UITableViewCells rows recycled and tag not workingiOS UITableViewCells 行被回收并且标签不起作用
【发布时间】:2012-07-26 20:37:05
【问题描述】:

我想在为单元格中的按钮设置标签方面寻求一些帮助。这是一个带有我之前发布的链接的问题:iOS Using NSDictionary to load data into section and rows

但是,虽然我现在可以动态传递数据,但我在每一行上的续订按钮似乎无法获取数据,并且只会在选择该部分中的任何行时检测到每个部分的相同书名.

根据我目前所阅读的内容,这是因为按钮正在被回收,因此无法检测到正确选择了哪本书。我试过设置标签:

cell.renewButton.tag = indexPath.row;

我的代码现在的样子:

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

static NSString *CellIdentifier = @"Cell";
UserCustomCell *cell = (UserCustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.bookTitle.frame = CGRectMake(12, 0, 550, 40);

if (cell == nil) {
    [[NSBundle mainBundle] loadNibNamed:@"UserCustomCell" owner:self options:nil];
    cell = userCustomCell;
    self.userCustomCell = nil;
    cell.renewButton.tag = indexPath.row;
}

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
    cell.bookTitle.frame = CGRectMake(12, 0, 550, 40);
    cell.renewButton.frame = CGRectMake(600, 14, 68, 24);
}
[cell.renewButton useBlackActionSheetStyle];

//########## 编辑从这里开始 dataSource = [[NSMutableDictionary alloc] init]; // 这需要是一个 ivar

for (NSDictionary *rawItem in myArray) {
    NSString *date = [rawItem objectForKey:@"date"]; // Store in the dictionary using the data as the key

    NSMutableArray *section = [dataSource objectForKey:date]; // Grab the section that corresponds to the date

    if (!section) { // If there is no section then create one and add it to the dataSource
        section = [[NSMutableArray alloc] init];
        [dataSource setObject:section forKey:date];
    }

    [section addObject:rawItem]; // add your object
}
self.dataSource = dataSource;
//NSLog(@"Data Source Dictionary: %@", dataSource); 

NSArray *sections =[[dataSource allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
NSString *sectionTitle = [sections objectAtIndex:indexPath.section];

NSArray *items = [dataSource objectForKey:sectionTitle];

NSDictionary *dict = [items objectAtIndex:indexPath.row];
cell.bookTitle.text = [dict objectForKey:@"name"];
cell.detail.text = [NSString stringWithFormat:@"Due Date: %@ Due Time: %@", 
                    [dict objectForKey:@"date"], [dict objectForKey:@"time"]];


cell.renewButton.tag = indexPath.row;
return cell;   

}

但它根本不起作用。非常感谢任何建议:) 谢谢!!

P.S:我的 xcode 副本没有更新,只更新到 version4。看到有人提到在 DataModel 中存储标签状态,但它仅在较新版本中可用。 :)

【问题讨论】:

  • 请显示您的整个“索引路径单元格”方法,如果您正确执行该方法,您应该能够使用标签。

标签: ios ios4 dynamic uitableview tags


【解决方案1】:

您不能使用按钮标签,因为这些标签与回收它们的单元格的标签相同。相反,使用indexPath 来确定您在哪一行并直接使用它。无需通过按钮标签。

【讨论】:

    【解决方案2】:

    我看不到您的 cell.renewButton 被分配了选择器方法(应该在点击按钮时触发的方法)。

    [cell.renewButton addTarget:self action:@selector(renewButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    

    另外,我会指定一个带有偏移量的标记号,因为标记为 0 几乎就像根本没有标记一样。 tableView 的第一行将给出 indexPath.row = 0。

    在您的代码之上,

    #define OFFSET 100 /* Or any number greater than 0 */
    

    在 cellForRowAtIndexPath 中,

    ...
    [cell.renewButton addTarget:self action:@selector(renewButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    cell.renewbutton.tag = indexPath.row + OFFSET;
    ...
    

    在renewButtonPressed方法中,

    -(void)renewButtonPressed:(id)sender
    {
        tappedNum = [sender tag] - OFFSET;
        /* do your stuff */
    }
    

    tappedNum 将为您提供点击按钮的行,从 0 开始。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-04-27
      • 2020-01-20
      • 1970-01-01
      • 1970-01-01
      • 2018-09-04
      • 2021-10-10
      • 1970-01-01
      相关资源
      最近更新 更多