getting data from each UITableView Cells Swift

First retrieve your cell where you need it (for instance in didSelectRowAtIndexPath). Cast your UITableViewCell in your custom cell. And then access properties of your cell as you wish.

Since you haven’t provided any code, I will provide simply examples of the code:

func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
    let cell = tableView.cellForRowAtIndexPath(indexPath) as YourCell
    //cell.value, cell.text, cell.randomValue or whatever you need
}

What about submit button, you want to submit the data right? And you don’t have indexPath there…

Well you have several options, one is to go through each cell, check the type and get it. But seems like your each cell is different one. And seems like you have order for these cells. So you know where exactly your result will be. So, in submit you can do the following

@IBAction func submit_pressed(sender: UIButton) {
     var indexs = NSIndexPath.init(index: 10)
     // or which one(s) you need. you extract data from the cell similar to previous function
}

But, why do you have to get entire cell to get one value? How about you create few variables (or even better, array) and extract values there? you can link events to these controls and when they change you get these value and save them. Later on, you can use these values(or array) without accessing cells or retrieving them.

EDIT:

How about tags? I am not sure if you are adding this through code or storyboard, but I will go through both of them.

In cellForRowAtIndexPath, you can simply say cell.tag = indexPath.row, or even better: cell.tag = question.id ( assuming that question is your custom class). That way, you can go through questions and take specific ones.

Here is working code with tags:

    @IBAction func test(sender: AnyObject) {
        var lbl : UILabel = self.tableView.viewWithTag(12) as UILabel!
        var cell : UITableViewCell = self.view.viewWithTag(3) as UITableViewCell!
        return ;
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = indexPath.row == 0 ? tableView.dequeueReusableCellWithIdentifier("firstCell", forIndexPath: indexPath) as UITableViewCell : tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) as UITableViewCell
        if(indexPath.row != 0){
        let item = toDoList[indexPath.row-1]

        cell.textLabel?.text = item.title
        NSLog("%d",indexPath.row)
        var integerncina = (indexPath.row + 10) as NSInteger!
        cell.textLabel?.tag = integerncina
        NSLog("%d", cell.textLabel!.tag)
        cell.tag = indexPath.row
        }
        // Configure the cell...

        return cell
    }

Leave a Comment