UITabBarController moreNavigationController 表格单元格背景图像
哇,那是一口吗? :)
我们已经在自定义更多菜单上看到了一个很好的线程(表视图)在标签栏中。
对于我们的下一个技巧……如何向表格单元格添加背景图像?
我认为我可以在“更多表视图”的数据源类中重写 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里就是答案...
添加代码
方法
Here is the answer...
Add the code in
method of MoreTableViewDataSource
除了 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.