3

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
                ...
  • Kurvirk you might be it will help you. https://stackoverflow.com/questions/28894765/uibutton-action-in-table-view-cell/41374087#41374087 – kalpesh Apr 17 '18 at 06:39

1 Answers1

1

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

RomOne
  • 2,065
  • 17
  • 29
  • i have a purpose on that actually :D –  Apr 17 '18 at 03:50
  • ahah ok, fair enough! Don't forget to accept the answer if it was the right one ;) Out of curiosity, what is the purpose of the `-1` ? – RomOne Apr 17 '18 at 03:51
  • :D, so you really wanted to know . okay , actually i am using index 0 for an add button aside from that index other index will have a dynamic values –  Apr 17 '18 at 03:53
  • since now i am no longer using that add button in the index 0 if i tap it it will now show the data cause that index is intended for that button –  Apr 17 '18 at 03:54
  • since i need index 0 , i set it to (-1) so that it will ignore it so i that i can still view item which index is 0 –  Apr 17 '18 at 03:55
  • this was the original if indexPath.row == 0 { // get a reference to our storyboard cell let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "AddNewCell", for: indexPath as IndexPath) as! AddNewCollectionViewCell cell.backgroundColor = unselectedCellColor return cell –  Apr 17 '18 at 03:56
  • ohhh ok I think I get it. Maybe you are confusing the tag from the index path. A cell will never have an index path with a negative row. But it's possible for a tag. – RomOne Apr 17 '18 at 03:57
  • Ahah no worries, now you know where you problem is coming from, you can work on it ;) – RomOne Apr 17 '18 at 03:59
  • Thanks Again , actually i am just really new to swift , if you need help in back node i think i could help you. –  Apr 17 '18 at 04:00
  • Great Principle Man. –  Apr 17 '18 at 05:04