【问题标题】:Need to get a logic for implementing the tableview sections according to array count需要根据数组计数获取实现 tableview 部分的逻辑
【发布时间】:2022-01-22 14:49:22
【问题描述】:

我的场景是我有三种不同类型的数组,它们可能包含也可能不包含值。我的表格视图有 3 个带有部分标题的部分。我很难找到动态设置部分的解决方案,即,如果我的一个数组没有值,那么我不想显示该部分。如果 3 个数组有值,则显示 3 个部分,或者如果任何一个数组没有值,则我不想显示该部分。

【问题讨论】:

  • 首先使用表示节的结构而不是数组,以便更好地通过名称或 id 识别。然后,如果您要删除一个部分的最后一项,请同时删除该部分。反之亦然:如果您要插入一个项目并且没有相应的部分,则也插入该部分。这比numberOfSectionsnumberOfRows 中的大量检查更有效。
  • @vadian 感谢您的评论,但我没有正确理解您。我有 3 个数组,其中每个数组都是不同的类型。你能帮我举个例子吗?
  • 首先显示你尝试过的代码

标签: ios arrays swift uitableview


【解决方案1】:

您的 numberOfSections 将是数组的数量。 numberOfRowsInSection 将是 tableViewDataSource 中该部分的每个数组的计数。

func numberOfSections(in tableView: UITableView) -> Int {
    
    return 3
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    
    if section == 0 {
        return array1.count
    } else if section == 1 {
        return array2.count
    } else {
        return array3.count
    }
    
}

如果数组中没有项目,则该部分的行数为零。

【讨论】:

  • 如果没有数组计数,我也需要隐藏该部分
  • 如果你没有节头,应该没问题。
  • 我确实有节标题
  • 如果节中没有数据,请将表头高度设置为零,或者仅在节项可用时才显示节头。
【解决方案2】:

你可以这样做

// Create enum for simplifying the implementation.

enum SectionType{
case type1
case type2
case type3
}

class TestVC: UIViewController {
// create to show sections only when data is available
var sections: [SectionType] = []
// create you array types
var array1 = [String]()
var array2 = [String]()
var array3 = [String]()
@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()
    
    tableView.dataSource = self
    // Add enums to section only when array has some value
    // You can do this when you get API data
    if array1.count > 0{
        sections.append(.type1)
    }
    if array2.count > 0{
        sections.append(.type2)
    }
    if array3.count > 0{
        sections.append(.type3)
    }
}

}
extension TestVC: UITableViewDataSource{
func numberOfSections(in tableView: UITableView) -> Int {
    // only those sections with data wil be visible
    return sections.count
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    
    switch sections[section]{
        
    case .type1:
        return array1.count
    case .type2:
        return array2.count
    case .type3:
        return array3.count
        
    }
    
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    switch sections[indexPath.section]{
        // return different type of cells if required accourding to the data in array
    case .type1, .type2, .type3:
        return UITableViewCell()
    

    }

}
  }

【讨论】:

    【解决方案3】:

    如果数组可以动态变化(在视图加载后),你可以实现这样的部分:

    func numberOfSections(in tableView: UITableView) -> Int {
      var numberOfSections = 0
      if array1.count > 0 { numberOfSections++ }
      if array2.count > 0 { numberOfSections++ }
      if array3.count > 0 { numberOfSections++ }
      return numberOfSections 
    }
    

    【讨论】:

      【解决方案4】:

      您的数据源应如下所示 - sectionModels = [[cellModels]] 外部数组表示节数,内部数组表示该节中的单元格数。

      func numberOfSections(in tableView: UITableView) -> Int {
          return sectionModels.count
      }
      
      func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
          return sectionModels[section].count            
      }
          
      }
      func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
          let cellModel = sectionModels[indexPath.section][indexPath.row]
          // Configure UITableViewCell with cellModel
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-09-27
        • 2018-03-18
        • 2011-08-25
        • 1970-01-01
        • 2022-09-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多