使用相同标签访问自定义 UITableViewCells 中的对象?

发布于 2024-12-22 03:37:32 字数 264 浏览 5 评论 0原文

在我的应用程序中,我使用从 XIB 加载的自定义 UITableViewCell。每个单元格包含 4 个对象,例如 UIImageView、UILabel、UITextField 等...

我在 cellforrowatindexpath 中设置了每个对象的标签,因此每个对象都具有相同的标签,该标签也是表视图中单元格的行。

问题是,如果单元格中的这些对象都具有相同的标签,我该如何访问它们?如何在没有 NSIndexPath 参数的方法中正确访问这些对象?

谢谢!

In my app, I use custom UITableViewCells loaded from a XIB. Each cell contains 4 objects, like a UIImageView, UILabel, UITextField, etc...

I set the tag of each object in my cellforrowatindexpath so each object has the same tag which also is the row of the cell in the tableview.

The problem is, how am I suppose to access these objects in a cell if they all have the same tag? How would I properly access these objects in a method that doesn't have a NSIndexPath parameter?

Thanks!

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

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

发布评论

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

评论(3

止于盛夏 2024-12-29 03:37:32

我个人尝试避免使用标签,因为我通常过度使用它们,以至于完全混乱,并开始寻找更优雅的方法来解决它。例如,在您的情况下,我会为自定义 UITableViewCell 的对象创建 IBOutlet ,使它们在语义上更加相关。然而,你的情况可能会有所不同 - 问题中没有足够的内容让我说这肯定适合你。

也就是说,听起来您可以通过以下方式完成您想要做的事情:

  1. 为 cellForRowAtIndexPath 方法返回的每个单元格提供一个标签(在返回它之前)为
  2. 单元格中的每个对象提供一个标签,正如您似乎已经完成的

那样,您可以通过以下方式从 UITableViewController 中的(任何地方)访问单元格的对象(大概是 UIViews):

UITableViewCell *cell = (UITableViewCell *)[self.tableview viewWithTag:CELL_TAG];
UIView *objectInCell = [cell viewWithTag:OBJECT_TAG];

请记住,这会让你访问特定单元格中的对象。如果您尝试访问所有单元格的对象,您可以简单地遍历所有这些对象。然而,我感觉此时这将开始变得不必要的复杂。如果您发布更多有关您通过这种方式使用标签到底想要实现什么目标的信息,将会有所帮助。希望这同时有所帮助。

I personally try avoiding the use of tags as I usually overuse them to the point of utter confusion and have begun finding more elegant ways around it. For instance, in your situation I would create IBOutlets for the custom UITableViewCell's objects making them more semantically relevant. However, your situation might be different - there is not enough in the question for me to say this would definitely work for you.

That said, it sounds like you could accomplish what you're trying to do by:

  1. Giving each cell returned by the cellForRowAtIndexPath method a tag (before returning it)
  2. Giving each of the objects within the cell a tag, as you seem to have already done

That way you can access the cell's objects (presumably UIViews) from (anywhere) in your UITableViewController by:

UITableViewCell *cell = (UITableViewCell *)[self.tableview viewWithTag:CELL_TAG];
UIView *objectInCell = [cell viewWithTag:OBJECT_TAG];

Keep in mind that this would allow you to access an object in a specific cell. If you were trying to access the objects of all your cells, you could simply iterate through all of them. However, I sense that this would start becoming unnecessarily complicated at this point. It would help if you posted up a bit more information on what exactly you were trying to achieve by utilising the tagging this way. Hope this helps in the meantime.

棒棒糖 2024-12-29 03:37:32

数字对一行中的所有标签进行编号,但我想你有你的理由([cell1 viewwithTag:0] 和 [cell2 viewWithTag: 0] 应该足以区分它们。)

不知道为什么人们会用相同的 元素使用相同的标签通常会违背最初标记它们的目的,但如果您必须在标签中包含行号(并且对结果集的大小有一定的了解),则可以使用 ( (多种的大小) + item# ) 整数(例如,如果您知道表将容纳少于 10,0000 个结果,您可以将第一行中的每个元素标记为 10001、10002、10003 和 10004,以及 20001、20002、20003 和接下来是 20004,等等...然后要寻址第 4 行中的第三个元素,您可以使用[查看标签:40003]。

No idea why one would number all the tags in a row with the same number, but I suppose you have your reasons ([cell1 viewwithTag:0] and [cell2 viewWithTag: 0] should be more than enough to differentiate them.)

Giving all the elements the same tag generally defeats the purpose of tagging them in the first place, though if you MUST include the row number in the tag (and have some idea about the size of the result set), you could tag each element with a ((multiple of size) + item# ) integer (e.g., if you know the table will hold fewer than 10,0000 results, you could tag each element as 10001, 10002, 10003 and 10004 in the first row, and 20001, 20002, 20003 and 20004 in the next, etc... Then to address the third element in the 4th row, you would use [view withTag: 40003].

夜空下最亮的亮点 2024-12-29 03:37:32

您可以使用此方法访问所选单元格的行:

NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]

但我不确定这是否回答您的问题。请说得更具体一些。

You can access the row of the selected cell with this methode:

NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]

but i am not sure if this answers your question. Please be more concrete.

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