【问题标题】:empty section (header view) with no cell in fetch result controller获取结果控制器中没有单元格的空部分(标题视图)
【发布时间】:2020-04-01 04:58:45
【问题描述】:

我正在寻找一种方法来创建一个没有单元格的空白部分。 我找到了这篇文章,但是它太复杂了,无法弄清楚。here

我有两个实体,分别称为 category 和 book 每个类别可以有很多书,但也可以没有书 我怎样才能让它工作? 这是我获取数据的方式

lazy var fetchResultController: NSFetchedResultsController<Book> = {
        let fetchRequest: NSFetchRequest<Book> = Book.fetchRequest()
        fetchRequest.sortDescriptors = [NSSortDescriptor(key: #keyPath(Book.name), ascending: false)]

        let fetchResultController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.persistentContainer.managedContext, sectionNameKeyPath: #keyPath(Book.category.name), cacheName: nil)

        fetchResultController.delegate = self

        return fetchResultController
    }()

这是 tableView 数据源

extension MainVC: UITableViewDataSource {

    func numberOfSections(in tableView: UITableView) -> Int {
        guard let sections = fetchResultController.sections else {
            return 0
        }
        return sections.count
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        guard let section = fetchResultController.sections?[section] else{return 0}

       return section.numberOfObjects
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: ID.TableView.mainPage) as! BookCell
        let book = fetchResultController.object(at: indexPath)

        cell.nameLbl.text = book.name

        return cell
    }
}

这是 tableView 委托

extension MainVC: UITableViewDelegate {

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        guard let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: ID.TableView.mainPageHeader) as? ProjectsHeaderView else{ return UIView() }

        let section = fetchResultController.sections?[section]


        header.label.text = section?.name
        header.delegate = self

        return header
    }
}

【问题讨论】:

    标签: ios swift xcode uitableview core-data


    【解决方案1】:

    您需要检查您的书籍数组的数量并相应地填充表格视图单元格。

        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 0 {
            return booksArray.count > 0 ? booksArray.count : 1
        } else {
            return categoryArray.count
        }
    }
    
     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
        switch indexPath.section {
        case 0 :
            if booksArray.count > 0 {
                  let cell : YourBooksCell = tableView.dequeueReusableCell(withIdentifier: "identifier", for: indexPath) as! YourBooksCell
                      return cell
                  } else {
                      let cell : YourEmptyCell = tableView.dequeueReusableCell(withIdentifier: "identifier", for: indexPath) as! YourEmptyCell
                                return cell
                  }
        default:
            let cell : YourCategoryCell = tableView.dequeueReusableCell(withIdentifier: "YourCategoryCell", for: indexPath) as! YourCategoryCell
            return cell
        }
    
    
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.section == 0 {
            if booksArray.count > 0 {
                return 250
            } else {
                return 100 // height for your empty cell
            }
        } else {
            return 100 // your category cell height
        }
    }
    
    1. 所以在 numberOfRowsInSection 中,我们检查 booksArray.count 是否为 0。如果为零,我们返回 1 行。这 1 行将是您的空行。

    2. 在您的 cellForRowAt 函数中,我们再次检查 booksarray 是否为 0。如果为零,我们返回空单元格,否则我们返回您的书单元

    3. 在 heightForRowAt 中,如果您的 booksArray 计数不为零,则使高度等于您的 books 单元格。否则,返回空单元格的高度。

    【讨论】:

    • 我正在使用 fetchResultController 所以我没有数组。我需要类别作为我的标题视图
    • 可以使用fetchedResultsController.fetchedObjects?.count来统计总行数
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-06
    • 2010-09-22
    • 1970-01-01
    相关资源
    最近更新 更多