user9606220
user9606220

Reputation:

swift access tableview indexPath ouside

I wanted to know how to access indexpath rows of a table view outside. I wanted to access on when a certain button is pressed which is outside. i have provided the code i have so far below , dont worry table is working fine just wanted to know how to access it outside. It keeps on returning index or cell# 0 when checkbuttonispressed.

wanted to access it here what i have so far

 func checkBoxButtonIsPressed(cell: ToDoListTableViewCell) {
        performSegue(withIdentifier: "updateChores", sender: nil)

        let indexPath = IndexPath(row: 0 , section: 0)
        let cell = tableView.cellForRow(at: indexPath)
        print("you selected cell #\(indexPath.item)!")
        //wanted to print selected row.


    }

what i want to access

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
          print("You selected cell #\(indexPath.item)!")


        if indexPath.row == (-1) {
//            var a = loggedInUsername
//            if ((a?.range(of: "mother")) != nil) {
////                performSegue(withIdentifier: "goToSegue", sender: nil)
//                print("yolo")
//            }else {
//                print("do nothing")
//                var alert = UIAlertController(title: "No Access", message: "You Can't Add A Chore", preferredStyle: UIAlertControllerStyle.alert)
//                alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil))
//                self.present(alert, animated: true, completion: nil)
//            }

        } else {


            print("You selected cell #\(indexPath.item)!")

            if let getTempDetails: [String : Any] = getAllDetail[indexPath.row],
                let name = getTempDetails["name"] as? String,
                let id = getTempDetails["id"] as? Int,
                let description = getTempDetails["desc"] as? String,
                let chorereward = getTempDetails["reward"] as? String,
                let choreschedule = getTempDetails["sched"] as? String
                ...

Upvotes: 3

Views: 532

Answers (1)

RomOne
RomOne

Reputation: 2105

So if I understand well you want to access the index path of the ToDoListTableViewCell.

You could do that way:

func checkBoxButtonIsPressed(cell: ToDoListTableViewCell) {
        performSegue(withIdentifier: "updateChores", sender: nil)

        let indexPath = tableView.indexPath(for: cell)
}

Btw, your code here:

if indexPath.row == (-1)

Will never work, as it's not possible to have a row with a -1 index

Upvotes: 1

Related Questions