Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep UITableViewCell in front of other cells

I am trying to keep the lower cells in front of the cells above them. I have a subview in a cell that would ideally go behind the cell below. I am having trouble finding where to put the code to do this. I think I am supposed to use this snippet of code:

for i in 0 ..< arrayWarningTitles.count{
        let cell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: i, inSection: 0))
        self.tableView.bringSubviewToFront(cell!)
}

but I am not sure where, as I have tried a few different places and none have worked (willDeselectRowAtIndexPath and didSelectRowAtIndexPath), and I would also like it on the initial load up as well. Any suggestions would be greatly appreciated!

UPDATE (Added Table View Code):

Here is the bulk of my Table View code, if it helps anyone, I have removed the above snippet as it is not working.

 override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    selection = indexPath.row
    tableView.beginUpdates()
    tableView.endUpdates()
    //  self.tableView
}

override func tableView(tableView: UITableView, willDeselectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? {
    tableView.beginUpdates()
    tableView.endUpdates()


    return indexPath;
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
    cell.selectionStyle = UITableViewCellSelectionStyle.None
    cell.backgroundColor = UIColor.clearColor()
    // Configure the cell...

    let warningTitle = self.view.viewWithTag(1) as? UILabel
    let expirationTime = self.view.viewWithTag(2) as? UILabel
    let textView = self.view.viewWithTag(5) as? UITextView
    let grayBackground = self.view.viewWithTag(6)
    let redBackground = self.view.viewWithTag(7)

    warningTitle?.text = arrayWarningTitles[indexPath.row]

    redBackground!.layer.masksToBounds = true;
    redBackground!.layer.cornerRadius  = 20;
    grayBackground!.layer.masksToBounds = true;
    grayBackground!.layer.cornerRadius  = 20;

    redBackground?.clipsToBounds = false;
    cell.clipsToBounds = false;
    cell.contentView.clipsToBounds = false;
    cell.contentView.superview?.clipsToBounds = false;
    self.tableView.bringSubviewToFront(cell)
    return cell
}

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{


    if (indexPath.row == selection) { //change 0 to whatever cell index you want taller
        return UIScreen.mainScreen().bounds.height - 140 - 64;
    }
    return 58;//Choose your custom row height
}
like image 227
JordanWx Avatar asked Jul 12 '16 18:07

JordanWx


1 Answers

It sounds as though you would be far better served with a a custom layout for UICollectionView.

I would recommend looking at a project such as this one and modifying the code in whichever way would serve you best.

like image 114
Infinity James Avatar answered Nov 09 '22 16:11

Infinity James