仅显示完全可见的UITableViewCells

时间:2015-07-08 09:48:45

标签: ios objective-c swift uitableview

我有一个自定义的tableView,但它的设计方式使得只有一半的单元格可见时底部和顶部都很难看。见图片参考:

tableView

我希望只有当你看到100%的细胞时,底部(和碾压后的顶部)才可见。

我试过这个来检查单元格是否完全可见,但我相信cellForRowAtIndexPath会在部分可见时创建reusableCells,并且当它完全可见时不会再次调用:

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

    var myCell:ChooseStoryCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! ChooseStoryCell

    var cellRect = self.tableView.rectForRowAtIndexPath(indexPath)
    var completelyVisible = CGRectContainsRect(self.tableView.bounds, cellRect)

    if completelyVisible == true {
        myCell.hidden = false
        myCell.backgroundColor = Color.sharedColors().colorsArray[5]
        myCell.storyLabel.text = stories[indexPath.row].name
        myCell.circleView.layer.cornerRadius = 15
    }
    else{
        myCell.hidden = true
    }

我将如何继续前进? 任何帮助都会受到极大的关注!

2 个答案:

答案 0 :(得分:1)

将表视图放在父UIView中。从tableview顶部到父视图顶部的边距应该等于单元格的高度,从底部开始,表视图的宽度应与其父视图的宽度相同,左右边距等于零。所以顶部/底部只有额外的空间来显示一个单元格。

在tableview上将clipsToBounds设置为NO,在父视图上确保clipsToBounds设置为YES。

行为应该是这样的,当滚动单元格时,它将一直可见,直到它到达父视图的顶部边界并立即消失。

或者,您也可以降低tableview的高度,将其向下移动并将clipsToBounds设置为NO。它应该这样做。但我更喜欢嵌入View以确保不会在外面显示任何内容。

答案 1 :(得分:1)

这是一个刚刚出现的代码。未经测试。

基本思想是查看单元格框架与表格视图边界的交集,如果结果是单元格的框架,则单元格框架完全可见。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var myCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell

        var cellRect = tableView.rectForRowAtIndexPath(indexPath)
        var completelyVisible = cellRect.rectByIntersecting(tableView.bounds) == cellRect

        if completelyVisible == true {
            myCell.contentView.hidden = false
        }
        else{
            myCell.contentView.hidden = true
        }

        return myCell
    }
相关问题