表视图行中的按钮可以以某种方式建立索引吗?

发布于 2024-12-27 06:11:52 字数 1831 浏览 3 评论 0原文

我有一个包含 3 个项目的表格视图,其中一个项目位于按钮后面。选择按钮后,我想隐藏该按钮,显示其后面的项目。我正在使用表视图单元格显示表行。当我选择要隐藏的一个按钮时,滚动表格会隐藏更多按钮。按钮的隐藏似乎是根据当前视图的可视行中的某个位置来隐藏按钮。我试图隐藏特定行上的按钮。

每当我点击隐藏按钮的代码时,我都可以写入 NSLog,而且我只会到达那里一次,但当我滚动表格时,按钮的隐藏属性适用于进入视图的其他行。如果我选择第 53 行上的按钮,我只想隐藏第 53 行中的按钮,而不是 120 行表中其他行上的按钮。

有人做过我想做的事吗?如果我能得到任何帮助来弄清楚发生了什么,我将不胜感激。谢谢。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *ElementCellIdentifier = @"ElementCellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ElementCellIdentifier];
    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ElementRowCell"
                                                 owner:self options:nil];
        if ([nib count] > 0) {
            cell = self.tvCell;
        } else {
            NSLog(@"failed to load ElementRowCell nib file!");
        }
    }

    NSUInteger row = [indexPath row];

    UILabel *atomic_number = (UILabel *)[cell.contentView viewWithTag:1];                               
    atomic_number.text = [NSString stringWithFormat:@"%d",elements_table[row].atomic_number];

    UILabel *element_name = (UILabel *)[cell.contentView viewWithTag:2];
    element_name.text = [NSString stringWithCString:elements_table[row].element_name];

    UILabel *element_symbol = (UILabel *)[cell.contentView viewWithTag:3];
    element_symbol.text = [NSString stringWithCString:elements_table[row].element_symbol];

    return cell;
}

- (IBAction)buttonPressed:(id)sender {

NSLog(@"Getting to buttonPressed from row button");
UIButton *pressedButton = (UIButton *)sender;
NSIndexPath *indexPath = [self.mainTableView indexPathForCell:     (UITableViewCell *)[sender superview]];
pressedButton.hidden = TRUE;

}

I have a table view with 3 items, one of which I have behind a button. When the button is selected, I want to hide that button, revealing the item behind it. I am displaying the table row using a table view cell. When I select the one button to hide, scrolling through the table hides more buttons. The hiding of the button seems to hide a button based on some location within the viewable rows of the current view. I'm trying to hide the button on a specific row.

I can write to the NSLog whenever I hit the code to hide a button and I will only get there once, but as I scroll through the table, the hidden attribute for the button applies to other rows that come into view. If I select the button on row 53 I want only the button in row 53 hidden, not buttons on other rows in the 120 row table.

Has anyone ever done what I am trying to do? Any help I can get to figure out what is happening would be appreciated. Thanks.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *ElementCellIdentifier = @"ElementCellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ElementCellIdentifier];
    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ElementRowCell"
                                                 owner:self options:nil];
        if ([nib count] > 0) {
            cell = self.tvCell;
        } else {
            NSLog(@"failed to load ElementRowCell nib file!");
        }
    }

    NSUInteger row = [indexPath row];

    UILabel *atomic_number = (UILabel *)[cell.contentView viewWithTag:1];                               
    atomic_number.text = [NSString stringWithFormat:@"%d",elements_table[row].atomic_number];

    UILabel *element_name = (UILabel *)[cell.contentView viewWithTag:2];
    element_name.text = [NSString stringWithCString:elements_table[row].element_name];

    UILabel *element_symbol = (UILabel *)[cell.contentView viewWithTag:3];
    element_symbol.text = [NSString stringWithCString:elements_table[row].element_symbol];

    return cell;
}

- (IBAction)buttonPressed:(id)sender {

NSLog(@"Getting to buttonPressed from row button");
UIButton *pressedButton = (UIButton *)sender;
NSIndexPath *indexPath = [self.mainTableView indexPathForCell:     (UITableViewCell *)[sender superview]];
pressedButton.hidden = TRUE;

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

安穩 2025-01-03 06:11:52

对不起。
基本上发生的事情是您将按钮的实例隐藏在特定的表视图单元格中。问题是当它因另一行而出列时,没有任何东西可以恢复它的状态。如果您只是将其状态恢复为可见,那么您单击的行将会被忘记。您需要保存已单击的行,以便能够正确恢复 tableView:cellForRowAtIndexPath: 中的状态。

我将如何处理这个问题是声明一个 NSMutableSet *selectedIndexPaths; 。并用它来存储我选择的行。然后,当单击按钮时,将该 indexPath 添加到集合中,如下所示。

- (IBAction)buttonPressed:(UIButton *)button{
    if (![button isKindOfClass:[UIButton class]]) return;
    UIView *finder = button.superview;
    while ((![finder isKindOfClass:[UITableViewCell class]]) && finder != nil) {
        finder = finder.superview;
    }
    if (finder == nil) return;
    UITableViewCell *myCell = (UITableViewCell *)finder;
    NSIndexPath *indexPath = [self.mainTableView indexPathForCell:myCell];
    [selectedIndexPaths addObject:indexPath];
    button.hidden = TRUE;
    NSLog(@"IndexPathRow %d",indexPath.row);
}

现在,要在 tableView:cellForRowAtIndexPath: 中滚动时正确恢复状态,请使用 if 语句设置按钮的隐藏属性,如下所示:

buttonPropertyName.hidden = ([selectedIndexPaths containsObject:indexPath]);

Sorry.
Basically what's happening is you are hiding the instance of the button in that specific table view cell. The problem is when it gets dequeue'd for another row nothing is restoring it's state. And if you were to just restore it's state to visible then the rows you clicked would be forgotten. You will need to save the rows that have been clicked already to be able to properly restore state in tableView:cellForRowAtIndexPath:.

How I would handle this is declare an NSMutableSet *selectedIndexPaths;. And use this to store the rows I have selected. Then when the button is clicked add that indexPath to the set like so.

- (IBAction)buttonPressed:(UIButton *)button{
    if (![button isKindOfClass:[UIButton class]]) return;
    UIView *finder = button.superview;
    while ((![finder isKindOfClass:[UITableViewCell class]]) && finder != nil) {
        finder = finder.superview;
    }
    if (finder == nil) return;
    UITableViewCell *myCell = (UITableViewCell *)finder;
    NSIndexPath *indexPath = [self.mainTableView indexPathForCell:myCell];
    [selectedIndexPaths addObject:indexPath];
    button.hidden = TRUE;
    NSLog(@"IndexPathRow %d",indexPath.row);
}

Now to properly restore state when scrolling in tableView:cellForRowAtIndexPath: use an if statement to set the button's hidden property, like so:

buttonPropertyName.hidden = ([selectedIndexPaths containsObject:indexPath]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文