2015-05-24 54 views
0

我有一个tableview,它使用UITableViewAutomaticDimension来计算单元格高度。当我点击一个Twitter时,我试图让这些单元格扩展,但即使单元格恢复到正常大小,我仍然遇到了单元格扩展的问题。我希望每个单元格都以一个常量值(例如50)展开,但我似乎无法抓住选定的单元格高度并将其添加50,因为在cellForRowAtIndexPath之前调用了heightForRowAtIndexPath。任何人都可以了解我能做什么?如果我使用UITableViewAutomaticDimension,如何增加单元格的高度?

这里是我的代码:

var selectedIndexPath: NSIndexPath? = nil 

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
     if !hasImageAtIndexPath(indexPath) { 
     var cell = tableView.cellForRowAtIndexPath(indexPath) as! TimelineTableViewCell 
     } else { 
     var cell = tableView.cellForRowAtIndexPath(indexPath) as! TimelineTableViewCellImage 
     } 
     switch selectedIndexPath { 
     case nil: 
      selectedIndexPath = indexPath 
     default: 
      if selectedIndexPath! == indexPath { 
       selectedIndexPath = nil 
      } else { 
       selectedIndexPath = indexPath 
      } 
     } 
     tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic) 
    } 
    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
     let smallHeight: CGFloat = UITableViewAutomaticDimension 

//Here is where I'm having trouble. Cells have a height of 50 and not current height +50 
      let expandedHeight: CGFloat = UITableViewAutomaticDimension+50 
      let ip = indexPath 
      if selectedIndexPath != nil { 
       if ip == selectedIndexPath! { 
        return expandedHeight 
       } else { 
        return smallHeight 
       } 
      } else { 
       return smallHeight 
      } 
     } 

回答

0

-tableView:heightForRowAtIndexPath:应该总是返回UITableViewAutomaticDimension。其实你可以做self.tableView.rowHeight = UITableViewAutomaticDimension

若要使单元格更高,请更改-tableView:cellForRowAtIndexPath:中的单元格约束,以便您的内容约束更高或者单元高度常数约束更高。

相关问题