【问题标题】:Adding custom button in tableview with tag在带有标签的tableview中添加自定义按钮
【发布时间】:2012-12-10 03:17:26
【问题描述】:

大家好, 我正在尝试添加带有与 indexPath.row 相关的标签的自定义按钮。如果我不将新行插入到 tableview 中,我可以正确查看标签值。但是,当我插入新行时,如果插入的行不在 0 到 9 之间(iphone 5 可以显示),我的新行标签值不正确。在 iphone 上,我需要向下滚动才能看到。


但是,使用相同的代码,我可以在 ipad 上正确获取我的标签值。我不需要向下滚动 ipad 上的表格视图来查看我的所有行。我想知道它为什么会发生以及如何解决。

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

    static NSString *CellIdentifier = @"dialoguesTableCell";
    dialoguesCell *cell = [tableViewdequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
    {
        cell = [[dialoguesCell alloc] initWithStyle:UITableViewCellStyleDefault 
            reuseIdentifier:CellIdentifier];

    }

    UIButton *yourButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [yourButton setImage:[UIImage imageNamed:@"1StarBlank.png"]     forState:UIControlStateNormal];
    [yourButton setTitle:@"Button" forState:UIControlStateNormal];
    [yourButton addTarget:self action:@selector(buttonSelected:)   forControlEvents:UIControlEventTouchUpInside];
    yourButton.tag=indexPath.row;
    yourButton.frame = CGRectMake(5, 10, 40, 25);
    [cell addSubview:yourButton];
    return cell;


}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath    *)indexPath
{
   NSIndexPath *indexPathstoinsert = [NSIndexPath indexPathForRow:indexPath.row+1     inSection:section];
   NSArray *indexPathsToInsertArray = [NSArray arrayWithObject:indexPathstoinsert];
   [[self mainTableView] insertRowsAtIndexPaths:indexPathsToInsertArray withRowAnimation:UITableViewRowAnimationRight];

}

【问题讨论】:

  • 请不要在您的帖子中使用签名或标语,否则它们将被删除。详情请见FAQ

标签: ios button tags tableview


【解决方案1】:

这无法正常工作,因为 UITableView 重用单元格
当您向下滚动时,第一个单元格不再可见并被重复使用。

如果表格......“小”,您可以通过不重复使用单元格
但是来解决它,如果表格不只是几个条目而是一个大量数据您真的想改变自己的方式

为按钮分配标签:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

例如

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    UIButton *b = nil;
    for(UIView *v in cell.subviews) {
        if([v isKindOfClass:[UIButton class]]) {
            b = (UIButton*)v;
            break;
        }
    }

    b.tag = indePath.row;
}

在评论中您提到了另一个问题:按钮隐藏在您的 didSelectRow 方法中,然后在滚动后也会从其他单元格中消失。同样的问题:单元格对象被表格视图重用。不要将状态存储在可重复使用的单元格中!

取而代之的是一个“模型”树或数组来记住状态:文本、图像、标签、隐藏:是/否

NSArray *myTableContents

 NSMutableDictionary *d1 = [@{@"text:@"bla", @"hidden":@NO} mutableCopy];
 NSMutableDictionary *d2 = [@{@"text:@"bloo", @"hidden":@NO} mutableCopy];
 NSMutableDictionary *d3 = [@{@"text:@"foo", @"hidden":@NO} mutableCopy];
 myTableContents = @[d1,d2,d3];

那么总是在 numberOfRows 和 viewForRow 中使用那个数组并在 didSelectEntry 中修改它

【讨论】:

  • 谢谢。它工作。我想再做一个与这个案子有关的步骤。我试图在新插入的行中隐藏该按钮。但是,当我向上或向下滚动时,其他按钮被隐藏了。我该怎么办?---- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSIndexPath *favouriteIndexpath = [NSIndexPath indexPathForRow :indexPath.row inSection:section]; UITableViewCell *cellktl = [tableView cellForRowAtIndexPath:favouriteIndexpath]; UIButton *exerciseDate = [cellktl viewWithTag:indexPath.row];练习日期隐藏=是; }
  • 同样的原因,单元格在 didSelect 之后被重用。你需要一些“模型”来记住隐藏了哪些行,然后在 willDisplay 中重新应用它。我会再为你写一些代码
  • 谢谢。它会起作用的。您能否再展示一下如何使用该数组?我应该写 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {..........} 然后,我不知道什么是 viewForRow。我想知道如何使用它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-07
  • 1970-01-01
  • 2011-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多