如何在选择 UITableView 行时更改其颜色?

发布于 2024-10-18 22:48:40 字数 207 浏览 0 评论 0原文

我已经四处寻找这个问题的答案,但尚未找到有效的答案。我想要做的就是能够更改背景颜色或我选择的 UITableView 行的图像。

我习惯于在选择一行时转换视图,但这次我希望能够更改该行属性。我知道我想使用 indexPath.row 更改哪一行,但我不知道如何更改该行的外观,因为 cellForRowAtIndexPath 方法已经被调用!

我可以做什么来改变颜色?

I've looked around for answers to this but have yet to find one that works. All I want to do is to be able to change the background color, or image of the UITableView row that I select.

I am used to just transitioning views when you I select a row, but this time I want to be able to change that rows properties. I know which row I want to change, with indexPath.row, but I don't know how I would go about changing how the row looks because the cellForRowAtIndexPath method has already been called!

What can I do to change the color?

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

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

发布评论

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

评论(2

城歌 2024-10-25 22:48:40

您可以这样做:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //First you get the cell you want to change
    UITableViewCell* theCell = [tableView cellForRowAtIndexPath:indexPath];

    //Then you change the properties (label, text, color etc..) in your case, the background color
    theCell.contentView.backgroundColor=[UIColor redColor];

    //Deselect the cell so you can see the color change

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

}

只要确保在回收细胞时考虑到这种颜色变化即可。

如果您需要更多信息,请发表评论。

You would do it like this:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //First you get the cell you want to change
    UITableViewCell* theCell = [tableView cellForRowAtIndexPath:indexPath];

    //Then you change the properties (label, text, color etc..) in your case, the background color
    theCell.contentView.backgroundColor=[UIColor redColor];

    //Deselect the cell so you can see the color change

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

}

Just make sure you consider this color change when you recycle cells.

If you need more info just post a comment.

星光不落少年眉 2024-10-25 22:48:40

将变量设置为活动行:

int activeRow;

构建单元格时,检查它是否是活动行并设置背景颜色

if (indexPath.row == activeRow) {
    cell.setMyBackgroundColor = [UIColor myCoolColor];
} else {
    cell.setMyBackgroundColor = [UIColor normalColor];
}

即使在正常情况下也不要忘记设置背景颜色,因为它们会被重用。

在选择时(或在适当的时候),更新该特定单元格:

NSArray *paths = [NSArray arrayWithObject:indexPath];
[self.tableView reloadRowsAtIndexPaths:paths withRowAnimation:YES];

我建议使用此方法,因为可能存在导致表格刷新的事件,并且您可能不希望丢失背景颜色。如果您直接在选择时设置单元格,则可能会发生这种情况。

Set a variable to the active row:

int activeRow;

When building the cell check if it's the active row and set the background color

if (indexPath.row == activeRow) {
    cell.setMyBackgroundColor = [UIColor myCoolColor];
} else {
    cell.setMyBackgroundColor = [UIColor normalColor];
}

Don't forget to set the background color even on the normal case, since they are reused.

On selection (or whenever is appropriate), update that particular cell:

NSArray *paths = [NSArray arrayWithObject:indexPath];
[self.tableView reloadRowsAtIndexPaths:paths withRowAnimation:YES];

I would suggest this method as there may be events that cause the refresh of your table, and you may not want to lose the background color. This could happen if you just set the cell directly on selection.

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