如何为所选行设置UITableViewCellAccessory? (iPhone / ipad公司) [英] How can I set the UITableViewCellAccessory for a chosen row? (iPhone/iPad)

查看:154
本文介绍了如何为所选行设置UITableViewCellAccessory? (iPhone / ipad公司)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种为任何行设置 UITableViewCellAccessory 的方法。然而,问题是我需要能够在 UITableView 委托方法的外部进行。

I need a way of setting the UITableViewCellAccessory for any row. However the catch is that I need to be able to do it OUTSIDE of the UITableView delegate methods.

我有试过这个,但它没有显示配件。

I have tried this, but it doesn't show up the accessory.

[[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathWithIndex:1]] setAccessoryType:UITableViewCellAccessoryCheckmark];

如果它有任何区别我创建了 UITableView 在故事板中的IB中。数据是静态的,我使用的分组表格样式只有一个部分。

If it makes any difference I created the UITableView in IB in a storyboard. The data is static, and I'm using a grouped table style with only one section.

请有人帮帮我吗?

推荐答案

如果滚动表格视图以使有问题的单元格有时可能会滚出视图然后再返回视图,则应仅从内部管理该单元格的内容UITableView方法 cellForRowAtIndexPath:。原因是当重绘单元格时,tableview对象调用此方法以确保正确呈现可见单元格。 (不可见的单元格根本不需要渲染。)

If your table view is scrolled so the cell in question may sometimes scroll out of view and then back into view, you should manage the content of that cell only from within the UITableView method cellForRowAtIndexPath:. The reason is that when cells are redrawn, the tableview object calls this method to make sure that visible cells are properly rendered. (Cell that are not visible don't need to be rendered at all.)

这就是说,这是你应该处理单元格内容的地方,即使表格也是如此视图不会将有问题的单元格滚动到视图之外。如果在使用表视图时遵循此设计模式,它将使您的生活变得更加轻松。

That being said, this is where you should handle the cell content, even if the table view doesn't scroll the cell in question out of view. It wil lmake you life a lot easier if you follow this design pattern when working with table views.

在该方法中,您可以测试(例如,使用if语句) indexPath.section和indexPath.row的值,以便您可以按照所需的方式配置特定单元格。这包括放入配件。

Inside that method, you can test (using if statement, for example) the value of the indexPath.section and indexPath.row so that you can configure the specific cell the way you want it. This includes putting in the accessory.

这篇关于如何为所选行设置UITableViewCellAccessory? (iPhone / ipad公司)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆