【问题标题】:iOS Static TableView Selection identifyiOS 静态 TableView 选择识别
【发布时间】:2015-01-02 11:38:34
【问题描述】:

我在容器内的视图内有一个静态表视图。有 5 个单元格,每个单元格代表不同的服务。服务详细信息视图对所有 5 个都是通用的,因此将用于每个。

到目前为止,我只通过代码(使用数组、UITableViewDataSourceDelegate 等)实例化了表格视图。

使用标签直观地识别每个单元格。

有没有办法为每个单元格分配故事板 ID 之类的东西?甚至是确定单元格标签值以进行比较的过度杀伤方式?

【问题讨论】:

    标签: ios objective-c iphone uitableview


    【解决方案1】:

    如果您仅在表格视图中有静态 5 个单元格,这非常容易。

    只需要使用NSIndexPath识别单元格即可。

    UITableViewCell *cell1 = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    UITableViewCell *cell2 = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
    UITableViewCell *cell3 = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:0]];
    UITableViewCell *cell4 = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:3 inSection:0]];
    UITableViewCell *cell5 = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:4 inSection:0]];
    

    cellForAtIndexPath方法中分配标签

    label.tag = indexPath.row;
    

    并使用上面的单元格对象获得尊重标签。

    UILabel *label1 = (UITextField *)[cell1 viewWithTag:0];
    UILabel *label2 = (UITextField *)[cell2 viewWithTag:1];
    UILabel *label3 = (UITextField *)[cell3 viewWithTag:2];
    UILabel *label4 = (UITextField *)[cell4 viewWithTag:3];
    UILabel *label5 = (UITextField *)[cell5 viewWithTag:4];
    

    【讨论】:

    • 我在设置正确的委托时遇到了一些额外的问题(我认为我可以使用单个视图控制器作为视图控制器和包含的表视图的委托),但是我对这个问题的总体解决方案是也打开 indexPath.row,类似于您的答案,0 => cell1, 1 => cell2 等。
    猜你喜欢
    • 2012-05-13
    • 2012-08-02
    • 1970-01-01
    • 2010-12-23
    • 2017-07-08
    • 2014-12-08
    • 2017-07-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多