Search code examples
iosuitableviewswiftcell

for loop to check on cell.textlabel or cell.tag


can anyone please help me with how to access a specific cell with its tag or text label, i have a table view with a list of buttons above, those buttons filed with the thumbnail of the cell when ever the cell was clicked then also the background of the cell goes red.

when i click on the cell once again - when its red - the background goes white - default - also the thumbnail removed from the button above.

also when i click on the button the photo in it should disappear as well as the background of the cell itself, it should go white. and thats what i couldnt do .. can anyone help me with that, how to access the the cell to change its background when the cell.tag or cell. texlabel.text is given.

thanx in advance

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    var cell = myTable.dequeueReusableCellWithIdentifier("intrest") as UITableViewCell
    cell.tag = indexPath.row + 1
    cell.textLabel?.text = InterstArr[indexPath.row]
    var img = UIImageView(frame: CGRect(x: 10, y: 3, width: 40 , height: 40))
    img.image = UIImage(named:"\(InterstArr[indexPath.row]).png")


    cell.selectionStyle = UITableViewCellSelectionStyle.None
    cell.indentationLevel = 1
    cell.indentationWidth = 45
    cell.addSubview(img)
    return cell
}

I did it with the help of @natuslaedo ... i just stored the value of the index path when i clicked on the row .. then in my if statements to check on the 5 buttons above and deselect them, i took the indexpath stored and accessed the cell to change its color ... i initialise the index paths variables like this var firstIP:NSIndexPath!


Solution

  • First of all, add the following line to your didSelectRowAtIndexPath: function:

    yourTableView.selectRowAtIndexPath(indexPath, animated: true, scrollPosition: UITableViewScrollPosition.None)
    

    And target selector for your button is like as follows:

    func buttonPressed(sender: UIButton!) {
        // Code to remove photo from your button
        // ......
    
        // To remove background effect of cell
        let indexPaths:Array<NSIndexPath> = yourTableView.indexPathsForSelectedRows() as Array<NSIndexPath>
        // If you have implemented your own class which inherits from UITableViewCell, use its name while type casting and explicit typing
        for indexPath in indexPaths {
            let cell:UITableViewCell = yourTableView.cellForRowAtIndexPath(indexPath) as UITableViewCell!
            // REMOVE BACKGROUND EFFECT HERE (using cell)
        }
    }