我想从 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 方法。
最佳答案
就我而言,有两种方法可以从单元格中获取数据。
方法一
将必要的数据放入 UITableViewCell
的子类中。
class MyTableViewCell: UITableViewCell {
var property1: String = ""
var property2: Int = 2
}
方法 2。
将数据放在struct
或class
中,让它成为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]
// .....
}
关于ios - 从 UITableViewCell 获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50636957/