改变高度时错误的单元格

时间:2014-11-26 13:14:37

标签: ios uitableview swift

我正在尝试改变单个细胞的高度,这通常会是一块蛋糕,但我似乎有一种奇怪的行为。当我更改第一个单元格高度时,它会更改第二个单元格高度。这可能是什么原因?

func numberOfSectionsInTableView(tableView: UITableView!) -> Int {
    return 1
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int)  -> Int {
    return self.categroyArray.count
}


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

    var bottomLine:UIView = UIView(frame: CGRectMake(0, cell!.bounds.height, self.view.frame.width, 1))
    bottomLine.backgroundColor = UIColor(rgba: "#cecece")
    cell?.addSubview(bottomLine)


    if indexPath.row == 0 {
        var categoryScroll:HMSegmentedControl = HMSegmentedControl(frame: CGRectMake(0, 0, cell!.bounds.width, cell!.bounds.height))
        categoryScroll.sectionTitles = self.categroyArray
        categoryScroll.autoresizingMask = UIViewAutoresizing.FlexibleRightMargin | UIViewAutoresizing.FlexibleWidth
        categoryScroll.font = UIFont(name: "HelveticaNeue", size: 12)
        categoryScroll.selectionStyle = HMSegmentedControlSelectionStyleFullWidthStripe
        categoryScroll.selectionIndicatorColor = UIColor(rgba: "#b44a44")
        categoryScroll.selectedTextColor = UIColor(rgba: "#b44a44")
        categoryScroll.textColor = UIColor(rgba: "#666666")
        categoryScroll.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationDown
        categoryScroll.segmentEdgeInset = UIEdgeInsetsMake(0, 10, 0, 10)
        categoryScroll.selectionIndicatorHeight = 4
        cell?.addSubview(categoryScroll)

    }

    return cell!
}


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


    if indexPath.row == 0 {
        return 100
    } else {
        return 44
    }

}

插图:

enter image description here

1 个答案:

答案 0 :(得分:0)

你有一些问题:

  1. 每次调用cellForRowAtIndexPath:时,您都会向该单元格添加子视图。如果您将已有这些子视图的单元格出列,则此选项不正确。

  2. 您在同一方法中依赖于单元格的框架/边界。但是,表格视图此时没有设置单元格的框架。

  3. 您可以通过将此代码移出此方法并进入单元子类中的相应方法来解决这两个问题。

相关问题