Swift-表格视图-对多个数据重复不同部分的顺序

问题描述

我有一个包含四个不同部分的表格。从数据库获取数据并将其存储在数组中之后,我想在表视图中显示它们。这意味着,对于每个对象,我将提取信息并将其放入表格视图的正确部分。

描述起来更简单: 对象1

TableViewCell 1 TableViewCell 2 TableViewCell 3 TableViewCell 4

对象2 TableViewCell 1 TableViewCell 2 ..

目前,我的方法看起来像

 func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> 
 UITableViewCell {
 let participationObject = Participation(json: (hitsSource?.hit(atIndex: indexPath.row))!)
    checkIfUserAlreadyRatedForParticipationContent(participationObject: participationObject)
    
    if (indexPath.section == 0 || indexPath.section % 4 == 0) {
        let userProfileCell = tableView.dequeueReusableCell(withIdentifier: "userProfileCell",for: indexPath) as! ChallengeInfoUserTableViewCell
                     
        self.dataAccessService.fetchUserById(completionHandler: { (challengeOrganizer) in
            let cell = tableView.cellForRow(at: indexPath) as! ChallengeInfoUserTableViewCell
            userProfileCell.fullNameLabel.text = challengeOrganizer.firstName + " " + challengeOrganizer.lastName
            userProfileCell.fetchProfileImageOfUser(userObject: challengeOrganizer)
            userProfileCell.fetchNumberOfWonChallengesByOrganizerId(challengeOrganizer: challengeOrganizer)
            
            userProfileCell.starNumberLabel.text = challengeOrganizer.stars
            userProfileCell.delegate = self
        },uid: participationObject.userId)

        return userProfileCell
    }
           
    else if (indexPath.section % 4 == 1) {
        let challengeImageCell = tableView.dequeueReusableCell(withIdentifier: "challengeImageCell",for: indexPath) as! ChallengeInfoImageTableViewCell
        
        dataAccessService.fetchParticipationImageByParticipantIdChallengeId(completionHandler: { (participationImage) in
            let cell = tableView.cellForRow(at: indexPath) as! ChallengeInfoImageTableViewCell
            challengeImageCell.checkIfToAddOrRemovePlayIcon(participationObject: participationObject)
            challengeImageCell.setChallengeImage(challengeImage: participationImage)
            challengeImageCell.delegate = self
            challengeImageCell.moreButton.tag = indexPath.row
        },participantId: participationObject.userId,challengeId: challengeObject.id)

        return challengeImageCell
    }
           
    else if (indexPath.section % 4 == 2) {
        let commentCell = tableView.dequeueReusableCell(withIdentifier: "commentCell",for: indexPath) as! ChallengeContentDetailCommentTableViewCell
        commentCell.fetchCommentsByParticipationId(participationObject: participationObject)
        return commentCell
    }

func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
    return 1
}
      
func numberOfSections(in tableView: UITableView) -> Int {
    return (hitsSource?.numberOfHits())! * 4
}

我有两个问题: 1.)我总是从数据源中获得相同的对象 2.)调用时,让cell = tableView.cellForRow(at:indexPath)为! ChallengeInfoImageTableViewCell 在某些时候崩溃。

我的方法是否合法?干净吗?

解决方法

  1. 这取决于数据数组,而您忽略了section中的numberOfSections(in:)参数

  2. 从不cellForRow中异步检索数据,并通过调用tableView.cellForRow(at:更新单元格。单元格可以立即移出屏幕,因此没有cellForRow(at

注意:

(indexPath.section == 0 || indexPath.section % 4 == 0)中的第一个表达式是多余的。如果indexPath.section % 4为0,则section为0。