ios - 使用选定的 UIPicker 行作为自定义 UITableViewCell UILabel SWIFT 的文本

标签 ios uitableview swift uipickerview

几天来我一直在反对这个问题,尝试了很多不同的方法。我有一个带有包含两个 UILabel 的自定义单元格的 UITableView。选择单元格时,会显示一个 UIPicker,并设置一个引用哪个单元格被点击的变量。

在 UIPicker 中做出选择后,我需要获取该值并使用它来替换其中一个单元格标签的现有 .text。我尝试过使用 .viewWithTag 的几种不同方式(首先标记标签本身,然后标记整个单元格)但没有任何效果。

我的选择器 didSelect 是:

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        println("pickerView did select row \(row)")

        if(self.getStatus() == "newFirst"){
            switch (editingField){
            case 0:
                var l:NewOrderCell! = tableView.viewWithTag(editingField) as? NewOrderCell
                //the label in the custom cell is called 'Text'
                l.Text.text = pickerData[row]
                println("should be changing \(l.Text.text) into \(pickerData[row])")
            default:
                var l:NewOrderCell! = tableView.viewWithTag(editingField) as? NewOrderCell
                l.Text.text = pickerData[row]
            }
        }
        picker.hidden=true
    }

我的表格 cellForRowAtIndexPath 是:

var cell:NewOrderCell = tableView.dequeueReusableCellWithIdentifier("NewOrderCell") as NewOrderCell
                //label
                cell.Label.text = self.newOrder[indexPath.row]
                //text
                cell.Text.text = "Please Select"
                cell.tag = indexPath.row
                return cell

我知道我的目标,但无法掌握从其他方法引用表格中单元格的正确方法。感谢您的帮助

最佳答案

I have come up with a solution that works, but I feel is inelegant and hard to maintain: when the picker row is selected, that text is written to the array that was used to originally populate the table (overwriting the original item ),然后重新加载表格,以便出现新值。所以,它是这样的:

var truth: [String:String] = ["Key1":"","Key2":"","Key3":""]
var keys:[String] = ["Key1", "Key2","Key3"]

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:NewOrderCell = tableView.dequeueReusableCellWithIdentifier("NewOrderCell") as NewOrderCell
     //label
     cell.Label.text = keys[indexPath.row]
     //textfield
     if(truth[keys[indexPath.row]] == ""){
            cell.Text.text = "Please Select"
     }
     else{
            cell.Text.text = truth[keys[indexPath.row]]
     }
     return cell
}

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    truth[keys[editingField]] = pickerData[row]
    dispatch_async(dispatch_get_main_queue(), { () -> Void in
         self.tableView.reloadData()
    })

}

很高兴听到更好的解决方案

关于ios - 使用选定的 UIPicker 行作为自定义 UITableViewCell UILabel SWIFT 的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29404197/

相关文章:

ios - UITableView onItemSelect 内的 Swift UICollectionView

ios - 在 Class init() 方法内的函数内初始化变量

ios - 更改 UITableView 框架时奇怪的 UITableViewCell 动画

ios - 拉动刷新不执行刷新功能?

ios - 当内容高于 EstimatedSize (sizeHint) 时,为什么单元格不调整大小

ios - 红蜘蛛委托(delegate)未被召集

ios - 忽略子类 SKSpriteNode 触摸事件

android - 静态类型语言中的生命周期方法

ios - UITableView 自定义插入操作按钮 onSwipe

swift - 尽管已正确识别单元格,但无法使标识符错误的单元格出队