如何更改tableView节标题的zPosition?

时间:2015-05-13 16:00:38

标签: ios uitableview swift sectionheader zposition

我有一个tableView,当我在返回单元格之前调用viewForHeaderInSection时。我有这段代码:

headerCell.layer.zPosition = headerCell.layer.zPosition-1 

现在这个工作的意思是单元格滚动到我想要的节标题。但是单元格在框架底部有3个按钮,当这些按钮滚动(或者我应该说下)部分标题内容视图时,视图会阻止按钮,即使我仍然可以按下按钮看到它们,这是因为单元格内容视图是在标题视图上呈现的。

如果我进入视图层次结构,标题内容视图位于单元格视图上方,这是阻止按钮操作的原因。

有没有人有解决方法?或者知道如何将headerCell的Zposition更改为按钮后面?我尝试了很多东西,但我需要帮助。

func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    if (cellIdentifier == "vivrCell") {
        println(screenSize.width)
        if (screenSize.width < 370) {
            return 180
        }else{
            return 220
        }
    }else {
        return 0.0
    }
}

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    if (cellIdentifier == "vivrCell") {
        let headerCell = mainTable.dequeueReusableCellWithIdentifier("vivrHeaderCell") as vivrHeaderCell
        headerCell.productID = self.feedReviewResults![section]["product"]["id"].stringValue
        headerCell.cellDelegate = self
        headerCell.clipsToBounds = false
        headerCell.layer.zPosition = headerCell.layer.zPosition-1
        return headerCell
    }
    return nil
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    switch cellIdentifier {
        case "vivrCell":
            return self.feedReviewResults?.count ?? 0
        case "newCell":
            return 1
    default:
        return 1

    }
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete method implementation.
    // Return the number of rows in the section.
    return 1
}


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

    switch cellIdentifier{
        case "buzzCell":
            var buzzcell = mainTable.dequeueReusableCellWithIdentifier(cellIdentifier) as buzzCell
            return buzzcell
        case "vivrCell":
            var vivrcell = mainTable.dequeueReusableCellWithIdentifier(cellIdentifier) as vivrCell
            vivrcell.review = self.feedReviewResults![indexPath.section]
            vivrcell.reviewID = self.feedReviewResults![indexPath.section]["id"].stringValue
            if let state = self.feedReviewResults![indexPath.section]["current_helpful"].boolValue as Bool?{
                vivrcell.helpfullState = state
            }
            vivrcell.wishlistState = true 
            vivrcell.cellDelegate = self
            vivrcell.layer.zPosition = vivrcell.layer.zPosition
            vivrcell.backgroundView = nil
            vivrcell.contentView.backgroundColor = UIColor.clearColor()
            return vivrcell
        default:
            let noNewProductsView = UIView(frame: CGRectMake(0, 0, self.view.frame.width, self.view.frame.height))
            noNewProductsView.backgroundColor = UIColor.whiteColor()
            let checkBackLabel = UILabel(frame: CGRectMake(0, 0, 200, 60.0))
            checkBackLabel.numberOfLines = 3
            checkBackLabel.textAlignment = NSTextAlignment.Center
            checkBackLabel.center = CGPointMake(self.view.center.x, self.view.center.y-100)
            checkBackLabel.text = "Check back for new products!"
            noNewProductsView.addSubview(checkBackLabel)
            var newcell = mainTable.dequeueReusableCellWithIdentifier(cellIdentifier) as newCell
            newcell.contentView.addSubview(noNewProductsView)
            return newcell

    }
}

3 个答案:

答案 0 :(得分:0)

也许你需要的只是标题上的点击被忽略。在标题上设置userInteractionEnabled = NO。

答案 1 :(得分:0)

如果userInteractionEnabled为NO不起作用,因为您还在节标题上有按钮,请将其添加到标题的子类中:

// We don't want touches, but we do want our subviews to get them.
// (So userInteractionEnabled = NO won't work, that'll stop subviews getting touches)
-(id)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    id hitView = [super hitTest:point withEvent:event];
    return (hitView == self) ? nil : hitView;
}

答案 2 :(得分:0)

{{1}}