ios - 从 UITableViewCell 获取数据

标签 ios objective-c uitableview

我想从 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。
将数据放在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]
    // .....
}

关于ios - 从 UITableViewCell 获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50636957/

相关文章:

ios - 我可以使用 swift 在 iOS 中围绕 y 轴旋转一个对象,但是如何围绕一个轴旋转多个对象?

ios - 使用带有委托(delegate)回调的 NSURLConnection 和 sendAsynchronousRequest :queue:completionHandler:? 之间有区别吗

iphone - 如何使用 Storyboard在 iOS 中的 UIViewController 之间切换

c++ - 如何将值从 NSMutableDictionary 复制到 Map?

ios - 在类似 twitter 的客户端中重用 UITableViewCell(为每个自定义高度)

ios - 作者删除了破坏我项目的 cocoapods repo 协议(protocol)。如何使用备份的 repo 文件来修复?

ios - 仅使用密码而非 TouchID 在设备上进行本地身份验证

objective-c - 从 super View 中删除存储在数组中的 UIView

ios - 我的 iPad Storyboard上的详细 View 未填充

ios - 在弹出它转换到的 View Controller 之前,如何让单元格保持突出显示?