0

Good Morning

I've a table view with multipel selection enabled of maximum 5 cells .. above it there is a list of 5 buttons and images .. when the user clicked on a cell a photo shall appear in the first empty imageViewabove the table, and when the user clicks on that photo it should disaapear and the cell color return to clear or white color in my case.

my problem is, when i click on a photo which cell is not shown because of scrolling, the app crashes. can you please help me .. how can i check if the indexpath is shown or not because the problem happened when it is not

here is my code :

@IBAction func btnClicked (sender : UIButton)
{

    let tester = sender

    if (tester == btn1 && img1.image != nil)
    {
        img1.image = nil
        let cell1 = myTable.cellForRowAtIndexPath(img1IP)! as CustomCell
        selectedIndexPaths.removeObject(img1IP)
        configure(cell1, forRowAtIndexPath: img1IP)




        counter--

    }

    else if (tester == btn2 && img2.image != nil)
    {
        img2.image = nil

        let cell2 = myTable.cellForRowAtIndexPath(img2IP)! as CustomCell
        selectedIndexPaths.removeObject(img2IP)
        configure(cell2, forRowAtIndexPath: img2IP)




        counter--


    }
    else if (tester == btn3 && img3.image != nil)
    {
        img3.image = nil
        let cell3 = myTable.cellForRowAtIndexPath(img3IP)! as CustomCell
                    selectedIndexPaths.removeObject(img3IP)
        configure(cell3, forRowAtIndexPath: img3IP)




        counter--


    }
    else if (tester == btn4 && img4.image != nil)
    {
        img4.image = nil
        let cell4 = myTable.cellForRowAtIndexPath(img4IP)! as CustomCell

        selectedIndexPaths.removeObject(img4IP)
        configure(cell4, forRowAtIndexPath: img4IP)



        counter--


    }
    else if (tester == btn5 && img5.image != nil)
    {
        img5.image = nil
        let cell5 = myTable.cellForRowAtIndexPath(img5IP)! as CustomCell

        selectedIndexPaths.removeObject(img5IP)
        configure(cell5, forRowAtIndexPath: img5IP)




        counter--


    }

    else
    {
        println("its already empty")
    }

}


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

    cell.selectionStyle = .None

    cell.backgroundColor = UIColor.clearColor()
    configure(cell, forRowAtIndexPath: indexPath)
    return cell
}



func configure(cell: CustomCell, forRowAtIndexPath indexPath: NSIndexPath) {
    cell.lblInCell.text = InterstArr[indexPath.row]
    if selectedIndexPaths.containsObject(indexPath) {
        // selected
        cell.imgInCell.backgroundColor = UIColor.redColor()
    }
    else if (selectedIndexPaths.containsObject(indexPath) == false ) {
        // not selected
        cell.imgInCell.backgroundColor = UIColor.whiteColor()
        cell.lblInCell.backgroundColor = UIColor.whiteColor()
    }
}


 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    if selectedIndexPaths.containsObject(indexPath) {
        // deselect
        println("remove object")
        selectedIndexPaths.removeObject(indexPath)

        ////////////
        if (img1str == InterstArr[indexPath.row])
        {
            img1.image = nil
            println("remove 1 object")


        }
        else if ( img2str == InterstArr[indexPath.row])
        {
            img2.image = nil
            println("remove 2 object")


        }
       else if (img3str == InterstArr[indexPath.row])
        {
            img3.image = nil
            println("remove 3 object")


        }
       else if (img4str == InterstArr[indexPath.row])
        {
            img4.image = nil
            println("remove 4 object")


        }

       else if ( img5str == InterstArr[indexPath.row])
        {
            img5.image = nil
            println("remove 5 object")

        }

        counter--
       // println(counter)
    }
    else if !(selectedIndexPaths.containsObject(indexPath)) {


        // select
            if (counter <= 4){


                if (img1.image == nil)
                {
                    img1str = InterstArr[indexPath.row]
                    img1.image = UIImage(named: InterstArr[indexPath.row])

                    img1IP = indexPath
                    println("add 1 object")

                }

                else if (img2.image == nil)
                {
                    img2str = InterstArr[indexPath.row]
                    img2IP = indexPath
                    img2.image = UIImage(named: InterstArr[indexPath.row])

                    println("add 2 object")


                }

                else if (img3.image == nil)
                {
                    img3str = InterstArr[indexPath.row]
                    img3IP = indexPath
                    img3.image = UIImage(named: InterstArr[indexPath.row])

                    println("add 3 object")


                }

                else if (img4.image == nil)
                {
                    img4str = InterstArr[indexPath.row]
                    img4IP = indexPath
                    img4.image = UIImage(named: InterstArr[indexPath.row])

                    println("add 4 object")


                }

                else if (img5.image == nil)
                {
                    img5str = InterstArr[indexPath.row]
                    img5IP = indexPath
                    img5.image = UIImage(named: InterstArr[indexPath.row])

                    println("add 5 object")

                }


        selectedIndexPaths.addObject(indexPath)

        counter++

        }
        else if (counter > 4)
        {
            println("you selected 5 already")
        }
    }
    let cell = tableView.cellForRowAtIndexPath(indexPath)! as CustomCell
    configure(cell, forRowAtIndexPath: indexPath)
}
Wild Will
  • 75
  • 1
  • 1
  • 10
  • What error are you getting? – Haring10 Feb 12 '15 at 09:27
  • @JoshHarington `fatal error: unexpectedly found nil while unwrapping an Optional value` – Wild Will Feb 12 '15 at 09:28
  • Which line is giving the error? – Haring10 Feb 12 '15 at 09:30
  • @JoshHarington I just added the `@IBOutlet` function which is connected to the buttons .. the line is `let cell1 = myTable.cellForRowAtIndexPath(img1IP)! as CustomCell` in case that i clicked on the first button when its cell is not shown – Wild Will Feb 12 '15 at 09:33
  • @JoshHarington it is an `IndexPath`, and it gives an error says `CustomCell` is not convertable to `UItableViewCell` – Wild Will Feb 12 '15 at 09:38
  • try let cell1 : Custom Cell = myTable.cellForRowAtIndexPath(img1IP)! as CustomCell, and try using var... – Miknash Feb 12 '15 at 10:24
  • @NickCatib it didnt work :( the problem is when the indexPath is not shown .. how can i check if the indexpath is shown or not – Wild Will Feb 12 '15 at 10:45
  • If I get it right, you are trying to get all selected cells? Doesn't matter are they visible or not? – Miknash Feb 12 '15 at 11:28
  • For your specific question check this link: http://stackoverflow.com/questions/9831485/best-way-to-check-if-uitableviewcell-is-completely-visible – Miknash Feb 12 '15 at 11:29

1 Answers1

0

Ok then .. the problem was how to check if the indexPath is shown, so here is my answer for those who have the same problem, as an example i posted only one part of the function and you can repeat if (tester == btn1 && img1.image != nil) for the other buttons .. the line to check if the indexPath is visible is this one if ((myTable.cellForRowAtIndexPath(img1IP)) != nil) :

@IBAction func btn1Clicked (sender : UIButton)
{
    let tester = sender

    if (tester == btn1 && img1.image != nil)
    {
     if ((myTable.cellForRowAtIndexPath(img1IP)) != nil)
     {
        println("1 exist")
        let cell:CustomCell = myTable.cellForRowAtIndexPath(img1IP) as CustomCell!
        cell.imgInCell.backgroundColor = UIColor.whiteColor()
        img1.image = nil
        selectedIndexPaths.removeObject(img1IP)
        }

        else
        {
            img1.image = nil
            selectedIndexPaths.removeObject(img1IP)
        }

}

Wild Will
  • 75
  • 1
  • 1
  • 10