我无法将TableViewCell按钮连接到TableViewController!如何将TableViewCell中的值连接到TableViewController?

时间:2015-12-08 07:01:18

标签: ios swift uitableview cell viewcontroller

根据我的理解,我无法使用插座连接将单元格中的按钮连接到我的TableViewController。 但我可以使用操作连接将单元格中的按钮连接到我的TableView。这是我的巨大问题的根源。

在我的单元格中,我有一个textview,一个带有重叠按钮的imageView和一个发送按钮。 这就是细胞

enter image description here

我使用imagePicker将图像分配给imageView。必须从tableViewController打开imagePicker(它不能在单元格中打开)。

var MyImage = UIImage?()
var MyName = String()
var MyStatus = String()

// This is ImgButton that overlays the imageView
@IBAction func MyImageButtonAction(sender: AnyObject) {

// Select Image
    let imagePicker = UIImagePickerController()
    imagePicker.delegate = self
    imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
    imagePicker.allowsEditing = false
    self.presentViewController(imagePicker, animated: true, completion: nil)

}
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
// The image chosen by the user is now called MyImage
     MyImage = image
     self.dismissViewControllerAnimated(true, completion: nil)

}

现在,当点击发送按钮时,我将MyImage作为Parse.com的PFFile并将其发送到数据库。我不需要在此提供详细信息。这部分工作正常。

问题我不知道如何正确地将单元格连接到tableViewController,以便我可以将值从一个传输到另一个。我想从单元格中获取textView.text到tableViewController,以便从那里我可以将它与图像一起发送到数据库。另一个问题是,虽然用户可以选择图像并将其发送到数据库,但他们无法将所选图像放入其imageView

这是我尝试将单元格连接到tableViewController:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("MyStatusTableViewCell", forIndexPath: indexPath) as! MyStatusTableViewCell

    cell.MyName.text = MyName

    return cell
}

我希望他的代码意味着MyName现在是包含用户在单元格中的textView.text中输入的.text的变量。我尝试将MyName发送到数据库,但它是空的。

如何将tableViewCell中的textView.text导入我的数据库?

3 个答案:

答案 0 :(得分:1)

你可以设置UITableViewCell的每个元素的标签,然后你可以在cellForRowAtIndexPath中访问它,如

if let textfield = cell.viewWithTag(21) as? UITextField {
    textfield.text = MyName
}

答案 1 :(得分:0)

您可以创建IBAction而不是IBOutlet,在按钮操作中,您可以将发件人转换为位置并找到像这样的索引路径

 @IBAction func buttonClick(sender: AnyObject) {
    let btnPos: CGPoint = sender.convertPoint(CGPointZero, toView: self.tableView)
    let indexPath: NSIndexPath = self.tableView.indexPathForRowAtPoint(btnPos)!

    print("indexPath->\(indexPath.row)")
}

答案 2 :(得分:0)

我发现了UITableView cellForRowAtIndexPath方法中存在的问题。您必须加载以下自定义单元格: -

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCellWithIdentifier("MyStatusTableViewCell", forIndexPath: indexPath) as! MyStatusTableViewCell
if cell == nil{

//Here you have to load your custom cells like that below
let array = NSBundle.mainBundle().loadNibNamed("MyStatusTableViewCell", owner: nil, options: nil)
cell = array[0] as?  MyStatusTableViewCell
}
cell.MyName.text = "MyName" //Now it will set your textview value
return cell
}
相关问题