ios - 动态 tableViewCellHeights

标签 ios objective-c uitableview swift

我有一个包含 3 个单元格的 UITableView。标题、说明和附加单元格。我希望 descriptionCell 是动态的,以便 attachcell 底部触及 UIKeyBoard 我怎样才能实现这样的实现?目前我已经通过使用 NSNotifications 找到了 KeyBoard 的框架。 TitleCell 高度为 50,attachCell 高度为 40。

如何确保在不同的设备上 attachCell 底部与 UiKeyBoard 顶部的位置相同?

我不介意答案在 Obj-C 中。

enter image description here

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/

相关文章:

ios - 与核心数据的多对多关系: how to populate intersection data?

android - 如何使用钛在android中应用幻灯片动画?

objective-c - 通过桥接 header 导入 Obj-C 文件后出错

json - 追加不起作用

ios - 这是否在创建单元格时形成强大的引用循环?

ios - TableView 数据滞后

iOS:在 Instruments 中按下录音时应用程序崩溃

objective-c - 我无法打开邮件应用程序?

ios - xib 的 UIControl 在 ios 7.1 上崩溃

iphone - 添加按钮部分标题?