【问题标题】:Get data from UITableViewCell从 UITableViewCell 获取数据
【发布时间】:2018-06-01 06:00:49
【问题描述】:

我想从 UITableViewCell 获取数据。这是我的代码。

[selection CreateTableview:colorList withSender:sender  withTitle:@"Please Select" setCompletionBlock:^(int tag){

        NSLog(@"Tag--->%d",tag);
        NSLog(@"Selected Row %ld",(long)indexPath.row);
        updated_lenses = tag;

        [self.tableview reloadData];

    }];

我在这里获得了选定的索引,但我想在该单元格中获取值,我该如何获取它。? 注意:我没有使用 DidSelectRowAtIndexPath 方法。

【问题讨论】:

  • 显示你的 cellforrowcode

标签: ios objective-c uitableview


【解决方案1】:

就我而言,有两种方法可以从单元格中获取数据。

方法一。
将必要的数据放入UITableViewCell的子类中。

class MyTableViewCell: UITableViewCell {
    var property1: String = ""
    var property2: Int = 2
}

方法 2。
将数据放入structclass,作为table view的数据源。

struct CellData {
    var property1: String = ""
    var property2: Int = 0
}
var cellDataList: [CellData] = [] ///< This decide how many cell for table view.

// For UITableView DataSource & Delegate
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return cellDataList.count
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let data = cellDataList[indexPath.row]
    // .....
}

【讨论】:

    【解决方案2】:
    NSIndexPath *index = [NSIndexPath indexPathForRow:tag inSection:0];
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:index];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-15
      • 2020-09-30
      • 2013-03-20
      • 1970-01-01
      相关资源
      最近更新 更多