【发布时间】: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