将UITableViewCell保留在其他单元格之前

Jor*_*nWx 6 uitableview ios swift

我试图将较低的细胞保持在它们上方的细胞前面.我在一个单元格中有一个子视图,理想情况下会在下面的单元格后面.我无法找到将代码放在何处执行此操作.我想我应该使用这段代码:

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

但我不知道在哪里,因为我已经尝试了一些不同的地方,没有工作过(willDeselectRowAtIndexPath和didSelectRowAtIndexPath方法),我也希望它的初始加载起来为好.任何建议将不胜感激!

更新(添加表格视图代码):

这是我的表格视图代码的大部分,如果它可以帮助任何人,我已删除上面的代码片段,因为它不起作用.

 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
}
Run Code Online (Sandbox Code Playgroud)

Inf*_*mes 1

听起来好像使用自定义布局会更好UICollectionView

我建议查看一个像这样的项目,并以最适合您的方式修改代码。