Swift 如何在不更改模型的情况下将tableview划分为多个部分

Swift 如何在不更改模型的情况下将tableview划分为多个部分,swift,tableview,sections,Swift,Tableview,Sections,我正在尝试将节添加到UITableView。我能够创建部分并正确计算每个部分中的元素数量,单元/图像会重复所有部分中的数据 仅发布相关代码: 型号: struct Product:Equatable { let imagename: UIImage } var productarray = [ Product(imagename:#imageLiteral(resourceName: "blue")), Product( imagename:#ima

我正在尝试将节添加到UITableView。我能够创建部分并正确计算每个部分中的元素数量,单元/图像会重复所有部分中的数据

仅发布相关代码:

型号:

struct Product:Equatable {
    let imagename: UIImage
}

var productarray = [
    Product(imagename:#imageLiteral(resourceName: "blue")),
    Product( imagename:#imageLiteral(resourceName: "CakeImage")),Product(imagename:#imageLiteral(resourceName: "vectorlogo")),
    Product(imagename:#imageLiteral(resourceName: "PeasImge")),Product(imagename:#imageLiteral(resourceName: "castle")),
    Product( imagename:#imageLiteral(resourceName: "scoobydoo")),Product(imagename:#imageLiteral(resourceName: "ufo")),
    Product( imagename:#imageLiteral(resourceName: "wolfsky")),Product( imagename:#imageLiteral(resourceName: "universe")),
    Product(imagename:#imageLiteral(resourceName: "werewolf")),Product(  imagename:#imageLiteral(resourceName: "galaxy"))
]
视图控制器:

class ProductViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    let sections = ["Section A", "Section B","Section C", "Section D","Section E"]
    let rowspersection = [2,3,2,2,2]
    @IBOutlet weak var tableView: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.delegate = self
        tableView.dataSource = self
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return sections.count
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return rowspersection[section]
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let data = productarray[indexPath.row]
        let cell = tableView.dequeueReusableCell(withIdentifier: "ProductTableViewCell") as! ProductTableViewCell
        cell.imageView?.image =  data.imagename
        cell.myParent = self
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 44
    }
    
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        
        switch(section) {
        case 0: return "Section A"
        case 1: return "Section B"
        case 2: return "Section C"
        case 3: return "Section D"
        case 4: return "Section E"
        default: return ""
        }
    }
}
结果是:

解释:

模型“struct Product”中“productarray”中的图像为:

蓝色、卡基图像、矢量徽标、豌豆、城堡、史酷比、不明飞行物、沃尔夫斯基、宇宙、狼人、银河

但模拟器的最终产品是:

A部分:蓝色,卡基图像

B部分:蓝色、CakeImage、vectorlogo

C部分:蓝色,卡基图像

D部分:蓝色,CakeImage

E部分:蓝色,卡基图像

i、 e.图像/单元格正在重复,我不希望发生这种情况。如我所愿,我希望将这些部分放置在单元格/图像之间。请帮助我

我想要的是:-

A部分:蓝色,卡基图像

B部分:vectorlogo、PeasImge、castle

C部分:史酷比、飞碟、

D部分:沃尔夫斯基,宇宙

E部分:狼人,银河系


注意:我不想更改模型“struct Product”,因为这样我就必须更改我在这里没有提到的其他扩展代码。

您的问题是,每个部分都从第0行开始,并且考虑到每个部分没有固定的行数,您必须计算在以下产品系列中要提升多少位置:

func-getIndex(indepath:indepath)->Int{
var指数=0
对于0..UITableViewCell中的i{
让data=productarray[getIndex(indexPath:indexPath)]
将cell=tableView.dequeueReusableCell(标识符为“ProductTableViewCell”)设为!ProductTableViewCell
cell.imageView?.image=data.imagename
cell.myParent=self
返回单元
}
更好的解决方案不是为此使用数组,而是使用一个映射了如下部分的字典:

产品:

var productMap=[
0:[产品(imagename:#imageLiteral(resourceName:“蓝色”)),产品(imagename:#imageLiteral(resourceName:“CakeImage”)]
1:[产品(imagename:#imageLiteral(resourceName:“vectorlogo”))、产品(imagename:#imageLiteral(resourceName:“PeasImge”)、产品(imagename:#imageLiteral(resourceName:“castle”)],
2:[产品(imagename:#imageLiteral(resourceName:“scoobydoo”)),产品(imagename:#imageLiteral(resourceName:“ufo”)],
3:[产品(imagename:#imageLiteral(resourceName:“wolfsky”)),产品(imagename:#imageLiteral(resourceName:“universe”)],
4:[产品(imagename:#imageLiteral(resourceName:“狼人”)),产品(imagename:#imageLiteral(resourceName:“银河”)]
]
在VC中:

func numberOfSections(在tableView:UITableView中)->Int{
返回productMap.count
}
func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
返回productMap[部分]?。计数为0
}
func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
让data=productMap[indexPath.section][indexPath.row]
将cell=tableView.dequeueReusableCell(标识符为“ProductTableViewCell”)设为!ProductTableViewCell
cell.imageView?.image=data.imagename
cell.myParent=self
返回单元
}

您的问题是,每个部分都从第0行开始,考虑到每个部分没有固定的行数,您必须计算在产品阵列中前进的位置,如下所示:

func-getIndex(indepath:indepath)->Int{
var指数=0
对于0..UITableViewCell中的i{
让data=productarray[getIndex(indexPath:indexPath)]
将cell=tableView.dequeueReusableCell(标识符为“ProductTableViewCell”)设为!ProductTableViewCell
cell.imageView?.image=data.imagename
cell.myParent=self
返回单元
}
更好的解决方案不是为此使用数组,而是使用一个映射了如下部分的字典:

产品:

var productMap=[
0:[产品(imagename:#imageLiteral(resourceName:“蓝色”)),产品(imagename:#imageLiteral(resourceName:“CakeImage”)]
1:[产品(imagename:#imageLiteral(resourceName:“vectorlogo”))、产品(imagename:#imageLiteral(resourceName:“PeasImge”)、产品(imagename:#imageLiteral(resourceName:“castle”)],
2:[产品(imagename:#imageLiteral(resourceName:“scoobydoo”)),产品(imagename:#imageLiteral(resourceName:“ufo”)],
3:[产品(imagename:#imageLiteral(resourceName:“wolfsky”)),产品(imagename:#imageLiteral(resourceName:“universe”)],
4:[产品(imagename:#imageLiteral(resourceName:“狼人”)),产品(imagename:#imageLiteral(resourceName:“银河”)]
]
在VC中:

func numberOfSections(在tableView:UITableView中)->Int{
返回productMap.count
}
func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
返回productMap[部分]?。计数为0
}
func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
让data=productMap[indexPath.section][indexPath.row]
将cell=tableView.dequeueReusableCell(标识符为“ProductTableViewCell”)设为!ProductTableViewCell
cell.imageView?.image=data.imagename
cell.myParent=self
返回单元
}

我不明白您想要什么,您不希望每个部分重复前面的项目,而是继续显示前面的项目?@ItayBrenner-请参见编辑问题出在主题中:…不更改模型。这样做,而不是硬编码
numberOfRows
numberOfSections
创建套装可修改模型。更改模型。我不明白您想要什么,您不希望每个部分重复前面的项目