我有一个包含 3 个单元格的 UITableView。标题、说明和附加单元格。我希望 descriptionCell 是动态的,以便 attachcell 底部触及 UIKeyBoard 我怎样才能实现这样的实现?目前我已经通过使用 NSNotifications 找到了 KeyBoard 的框架。 TitleCell 高度为 50,attachCell 高度为 40。
如何确保在不同的设备上 attachCell 底部与 UiKeyBoard 顶部的位置相同?
我不介意答案在 Obj-C 中。
ViewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
self.title = "New"
self.tableView?.scrollEnabled = false
self.tableView?.registerNib(UINib(nibName: "DescriptionViewCell", bundle: nil), forCellReuseIdentifier: "DescriptionViewCell")
self.tableView?.registerNib(UINib(nibName: "AttachViewCell", bundle: nil), forCellReuseIdentifier: "AttachViewCell")
self.tableView?.registerNib(UINib(nibName: "TitleViewCell", bundle: nil), forCellReuseIdentifier: "TitleViewCell")
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name: UIKeyboardDidShowNotification, object: nil)
// Do any additional setup after loading the view.
}
KeyBoardWillShow
func keyboardWillShow(notification: NSNotification) {
keyboardFrame = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as NSValue).CGRectValue()
}
heightForRowAtIndexPath
func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat {
if indexPath.row == 0 {
return 50
}
else if indexPath.row == 1 {
return 150
} else {
return 40
}
}
最佳答案
警告:按照您的说法,您可能整天都在与 UIKit 作斗争。考虑到您必须针对不同设备的屏幕尺寸和方向自定义此解决方案。
话虽如此,本指南应该为您指明正确的方向:Managing the keyboard .
- (void)keyboardWasShown:(NSNotification*)aNotification {
和 [scrollView setContentOffset
应该提供解决方案。它还包含大量的 if, else if, else
语句。
我的拙见:与其关注美学,不如考虑可用性。
关于ios - 动态 tableViewCellHeights,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25871088/