ios - 静态和动态单元格的 UITableView 混合?

我知道您不能在单个 UITableView 中混合静态和动态单元格类型,但我想不出更好的方式来描述我的问题。

我有几个具有固定内容的预定单元格,我还有未知数量的具有动态内容的单元格位于中间。所以我想让我的 table 看起来像这样:

Fixed
Fixed
Fixed
Dynamic
Dynamic
Dynamic
Dynamic
Dynamic
Fixed
Fixed

那么你究竟建议我如何在我的 cellForRowAtIndexPath 方法中处理这个问题?

谢谢。

最佳答案

苦苦挣扎1天,找到最佳解决方案

为 Swift 4 更新

对于问题的情况:

  1. 将 tableView 的部分设置为 3

  1. 在要应用动态单元格的第二部分添加一个空的tableview单元格,将单元格的标识符更改为WiFiTableViewCell

  2. 创建一个名为 WiFiTableViewCell

  3. 的新 XIB 文件

  1. 在tableViewController的ViewDidLoad函数中注册Nib

    tableView.register(UINib(nibName: "WiFiTableViewCell", bundle: nil), forCellReuseIdentifier: "WiFiTableViewCell")
    
  2. 添加以下代码以同时使用动态和静态单元格

    override func numberOfSections(in tableView: UITableView) -> Int 
    {
        return 3
    }
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 1 {
            return self.dataSource.count
            //the datasource of the dynamic section
        }
        return super.tableView(tableView, numberOfRowsInSection: section)
     }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.section == 1 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "WiFiTableViewCell") as! WiFiTableViewCell
            return cell
        }
        return super.tableView(tableView, cellForRowAt: indexPath)
    }
    
     override func tableView(_ tableView: UITableView, indentationLevelForRowAt indexPath: NSIndexPath) -> Int {
         if indexPath.section == 1 {
             let newIndexPath = IndexPath(row: 0, section: indexPath.section)
             return super.tableView(tableView, indentationLevelForRowAt: newIndexPath)
         }
         return super.tableView(tableView, indentationLevelForRowAt: indexPath)
     }
    
     override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
         if indexPath.section == 1 {
             return 44
         }
         return super.tableView(tableView, heightForRowAt: indexPath)
      }
    

https://stackoverflow.com/questions/18153702/

相关文章:

iphone - Objective-C - 将 float 转换为字符串的其他方法

iphone - NSDateFormatter,我做错了什么还是这是一个错误?

ios - 切换隐私设置将杀死应用程序

ios - swift 中的@property/@synthesize 等价物

ios - 带有 AVPlayer 的多个视频

ios - 如何在 iOS 中使用 'Container View'?

objective-c - 为什么不鼓励使用伞式框架?

iphone - 如何以编程方式创建 UIScrollView?

objective-c - 在 Objective-C 中定义协议(protocol)的类别?

iphone - Objective-C 中的实例变量是否默认设置为 nil?