Lydia
Lydia

Reputation: 2117

Custom tableview cell - Need to getting selected the cell even press a custom button in the cell

I have a custom tableview.

class CustomCell: UITableViewCell {

var firstBtn  : UIButton!
var secondBtn : UIButton!

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)

    // button frame coding
    self.contentView.addSubview(firstBtn)
    self.contentView.addSubview(secondBtn)
}

In view controller i created tableview programatically.

colorTableView.frame = CGRectMake(20,50,100, 200)
colorTableView.delegate = self
colorTableView.dataSource = self
colorView.addSubview(colorTableView)

And the problem is when select a cell it highlight as gray color.But when i press a button in the same cell the highlight color disappears.So i cannot able to find which cell is selected.

I need to be remain selected the cell till go to next cell.

And I tried to fix it by manually give backgound color as follows but that not work as i wish.

Cell?.contentView.backgroundColor = UIColor.lightGrayColor()
Cell?.textLabel?.backgroundColor = UIColor.clearColor()

Cellforindexpath

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

    var cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! CustomCell


            cell.contentView.addSubview(cell.firstbutton)

        cell.firstbutton.addTarget(self, action: "buttonpress:", forControlEvents: UIControlEvents.TouchUpInside)
       }

Upvotes: 0

Views: 842

Answers (4)

Lydia
Lydia

Reputation: 2117

With God Grace finally i got the solution.

First Step: Store the selected row's indexpath

   func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? {
    println("row selected")
    GetIndexOfRow = indexPath

    return GetIndexOfRow
}

Then in cellForAtIndexPath Method:

          if getIndexpathOfRow != nil {
        if indexPath ==  getIndexpathOfRow {
            cell.contentView.backgroundColor = UIColor(white: 0.85, alpha: 1)
            cell.textLabel?.backgroundColor = UIColor(white: 0.85, alpha: 1)

        }else {
            cell.contentView.backgroundColor = UIColor.whiteColor()
            cell.textLabel?.backgroundColor = UIColor.whiteColor()
        }

    }

These gives a selection of row with light gray color as default selection color of tableview cell.

      for view in self.view.subviews {
        for each in view.subviews {

            if each.isKindOfClass(UITableView){
                var getTableview = each as! UITableView

                for (var j = 0; j < getTableview.numberOfSections(); ++j)
                {
                    for (var i = 0; i < getTableview.numberOfRowsInSection(j); ++i)
                    {

                                                                                var cells =  getTableview.cellForRowAtIndexPath(NSIndexPath(forRow: i, inSection: j))

                        if cells?.textLabel?.backgroundColor  == UIColor(white: 0.85, alpha: 1.0) {
                        cells?.contentView.backgroundColor = UIColor.whiteColor()
                        cells?.textLabel?.backgroundColor = UIColor.whiteColor()
                        }



                    }
                }


            }
        }


    }

The above code works as follows.When we select a row its check visible cells background cell color.If any-other cell has color means.Changed to clear color(In my case white color).So we can only see the selected cell highlight.

This is enough to the cell getting highlighted if even work outer buttons or button in the cell.

Hope it helps someone.

Upvotes: 0

Gopal Devra
Gopal Devra

Reputation: 614

I think you should not rely on selected index of row, you just use the tag property of button to find out the particular sender.

Upvotes: 0

Amit89
Amit89

Reputation: 3000

Here is a solution.

Create a variable like - var currentSelectedIndex: NSIndexPath!

func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
      if self.currentSelectedIndex != indexPath {
            let previosSelectedCell = tableView.cellForRowAtIndexPath(self.currentSelectedIndex) as! UITableViewCell
            selectedCell.backgroundColor = UIColor.clearColor()
            self.currentSelectedIndex = indexPath
            let selectedCell = tableView.cellForRowAtIndexPath(indexPath) as! UITableViewCell
            selectedCell.backgroundColor = UIColor.redColor()
        }
}

Upvotes: 1

derdida
derdida

Reputation: 14904

  1. Write a custom function for your button, for example (use Button as sender)

  2. Your button function:

    func buttonpress(sender: UIButton) {
    // get the cell and table
        var cell: UITableViewCell = sender.superview.superview as UITableViewCell
        var tableView: UITableView = cell.superview as UITableView
        let rowToSelectIndexPath = tableView.indexPathForCell(cell)
        tableView.selectRowAtIndexPath(rowToSelectIndexPath, animated: true, scrollPosition:UITableViewScrollPosition.None)
    }
    

Upvotes: 0

Related Questions