【问题标题】:Swift - Table View - Repeat order of different sections for multiple dataSwift - 表格视图 - 为多个数据重复不同部分的顺序
【发布时间】:2020-11-03 18:11:31
【问题描述】:

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

描述得更简单: 对象 1

TableViewCell 1 表视图单元 2 表视图单元 3 TableViewCell 4

对象 2 表视图单元 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.) 当调用 let cell = tableView.cellForRow(at: indexPath) as! ChallengeInfoImageTableViewCell它在某个时候崩溃了。

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

【问题讨论】:

    标签: swift uitableview asynchronous custom-cell


    【解决方案1】:
    1. 这取决于数据数组,您忽略了numberOfSections(in:) 中的section 参数

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

    注意:

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

    【讨论】:

    • 你好@vadian。谢谢回复。我不明白如何以及为什么应该使用 section 参数?而且,我的数据数组包含不同的对象,但我只得到第一个。你的意思是什么取决于我的数据数组?关于第二点。我应该将异步加载移动到 ChallengeInfoImageTableViewCell 类本身吗?
    • 您只得到第一个,因为您在numberOfRowsInSection 中返回(固定)1。异步任务应该在 controllermodel 中执行,而不是在 view 中执行
    • 如何修改上面的代码以符合我的期望?我提供了 1 作为固定值,但无论我提供哪个值,它都显示错误。
    • 你必须重构你的代码。创建一个合适的数据模型,填充它 - Participation 的东西和依赖的网络任务 - 在 viewDidLoadviewWillAppearcellForRow 中只需更新 UI。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-26
    • 1970-01-01
    • 1970-01-01
    • 2016-12-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多