首页 > 解决方案 > UItableview 有两组选项

问题描述

我有一个表格视图,根据用户类型显示的内容略有不同。所以一个是狗主人的选项列表,另一个是遛狗者的选项列表。有些选项是相同的。最好的方法是一个表视图控制器?

Name
Address
My walkers
My dogs
Name
Address
My customers
Walking qualifications

(在现实生活中,这些表要长得多)

标签: iosswiftuitableview

解决方案


创建两个不同的 tableview 单元格并根据用户类型更改单元格。这是一种做法。

func tableView(_ tableView: UITableView, cellForItemAt indexPath: IndexPath) -> UITableViewCell {
     if dataSource[indexPath.item].isFirstuser {
        let cell = tableview.dequeueReusableCell(withReuseIdentifier: “type1”, for: indexPath) as! type1Cell
        let model = dataSource[indexPath.item]
        cell.model = model
        return cell
      } else {
        let cell = tableView.dequeueReusableCell(withReuseIdentifier: “type2”, for: indexPath) as! type2Cell
        let model = dataSource[indexPath.item]
        cell.model = model
        return cell
       }   
}

推荐阅读