UITabBarController moreNavigationController 表格单元格背景图像

发布于 2024-08-07 10:27:09 字数 1133 浏览 3 评论 0 原文

哇,那是一口吗? :)

我们已经在自定义更多菜单上看到了一个很好的线程(表视图)在标签栏中

对于我们的下一个技巧……如何向表格单元格添加背景图像?

我认为我可以在“更多表视图”的数据源类中重写 tableView:cellForRowAtIndexPath: (有关方法,请参阅前面的链接)。遗憾的是,它仅更改每个单元格左侧和右侧图像和附件视图下的背景。

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

    // Let the original Data Source get a hold of the cell first.
    UITableViewCell *cell = [originalDataSource tableView:tableView cellForRowAtIndexPath:indexPath];

    // If we override the textColor, that works fine. (Commented out for now.)
    //cell.textLabel.textColor = [UIColor whiteColor];

    // If we override the background view … that doesn't work so well.
    cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cellBackground.png"]];

    return cell;
}

我知道, imageNamed: 是邪恶的,也许我应该创建一张背景图像并在整个过程中重复使用它(而不是分配多个 UIImageView 对象)。除了这些东西,还有什么想法吗? (我也尝试调整 textLabel 的背景颜色,但无济于事......除非我在那里做错了什么。)

Wow, was that ever a mouthful. :)

We've already seen a good thread on customizing the more menu (table view) in a tab bar.

For our next trick … how might one add a background image to the table cells?

I thought I could get away with overriding tableView:cellForRowAtIndexPath: in my More Table View's Data Source class (see earlier link for the methodology). Alas, it only changes the background under the image and accessory views to the left and right of each cell.

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

    // Let the original Data Source get a hold of the cell first.
    UITableViewCell *cell = [originalDataSource tableView:tableView cellForRowAtIndexPath:indexPath];

    // If we override the textColor, that works fine. (Commented out for now.)
    //cell.textLabel.textColor = [UIColor whiteColor];

    // If we override the background view … that doesn't work so well.
    cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cellBackground.png"]];

    return cell;
}

I know, imageNamed: is evil, and perhaps I should create one background image and just reuse it throughout (vs. allocating multiple UIImageView objects). Apart from those items, any thoughts? (I tried adjusting the textLabel's backgroundColor too, to no avail … unless I'm doing something wrong there.)

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

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

发布评论

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

评论(2

寄意 2024-08-14 10:27:09

这里就是答案...

添加代码

  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

方法

Here is the answer...

Add the code in

  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

method of MoreTableViewDataSource

魂ガ小子 2024-08-14 10:27:09

除了 Sijo 的回答帖子(谢谢!),我刚刚意识到还有 SO 上的另一个线程 采用了类似的策略......只是我对这段代码了解得更多一些。但是,我从未将其交叉引用回这个问题/线程!所以就是这样。特别参见 Ian1971 的回复。

In addition to Sijo's answer post (thank you!), I just realized there was another thread on SO that takes a similar tack ... except I understand this code a bit more. However, I never cross-referenced it back to this question/thread! So here it is. See Ian1971's response in particular.

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