Ios UITableView将行内容弄乱

Ios UITableView将行内容弄乱,ios,swift,Ios,Swift,我试图只复制必要的代码来显示我的问题。我有一个动态内容的tableview。我创建了一个原型单元,它有一个用户名和10颗星(这是一个评级页面)。允许组中的人对其他人进行评分。一切正常,但向下滚动时出现问题。如果我用8颗星给我的第一个用户打分,当我向下滚动时,在tableview底部区域的某个用户会显示我给我的第一个用户的评分。我知道tableview可以重用单元格。我尝试了很多事情,但没有成功。希望有人能帮我 func tableView(tableView: UITableView, cell

我试图只复制必要的代码来显示我的问题。我有一个动态内容的tableview。我创建了一个原型单元,它有一个用户名和10颗星(这是一个评级页面)。允许组中的人对其他人进行评分。一切正常,但向下滚动时出现问题。如果我用8颗星给我的第一个用户打分,当我向下滚动时,在tableview底部区域的某个用户会显示我给我的第一个用户的评分。我知道tableview可以重用单元格。我尝试了很多事情,但没有成功。希望有人能帮我

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let model = users[indexPath.row]

        let cell = tableView.dequeueReusableCellWithIdentifier("RatingCell") as! RatingTableViewCell
        cell.tag = indexPath.row

        cell.playerLabel.text = model.name

        cell.averageView.layer.borderWidth = 1
        cell.averageView.layer.borderColor = Color.Gray1.CGColor
        cell.averageView.layer.cornerRadius = 5

        cell.starsView.userInteractionEnabled = true

        cell.averageLabel.text = "\(user.grade)"

        for i in 0...9 {
            let star = cell.starsView.subviews[i] as! UIImageView
                star.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(starTap)))
            star.userInteractionEnabled = true
            star.tag = i
            star.image = UIImage(named: (i + 1 <= grade ? "star-selected" : "star-empty"))
        }
        return cell
}

func changeRating(sender: UIImageView) {
    let selectedStarIndex = sender.tag
    let cell = sender.superview?.superview?.superview as! RatingTableViewCell
    let model = users[cell.tag]
    let stars = sender.superview?.subviews as! [UIImageView]

    cell.averageLabel.text = "\(selectedStarIndex + 1)"

    for i in 0...9 {
        let imgName = i <= selectedStarIndex ? "star-selected" : "star-empty"
        stars[i].image = UIImage(named: imgName)
    }
}

func starTap(gesture: UITapGestureRecognizer) {
    changeRating(gesture.view as! UIImageView)
}
func tableView(tableView:UITableView,cellForRowAtIndexPath:nsindepath)->UITableView单元格{
让model=users[indexPath.row]
将cell=tableView.dequeueReusableCellWithIdentifier(“RatingCell”)设为!RatingTableViewCell
cell.tag=indexPath.row
cell.playerLabel.text=model.name
cell.averageView.layer.borderWidth=1
cell.averageView.layer.borderColor=Color.Gray1.CGColor
cell.averageView.layer.cornerRadius=5
cell.starsView.userInteractionEnabled=true
cell.averageLabel.text=“\(user.grade)”
因为我在0…9{
设star=cell.starsView.subview[i]为!UIImageView
star.addgestureRecognitizer(UITapgestureRecognitizer(目标:self,操作:#选择器(starTap)))
star.userInteractionEnabled=true
star.tag=i

star.image=UIImage(命名为:(i+1解决此问题的方法是更新保存uitableviewcell所有信息的模型。每当为特定单元格更新评级时,请确保在数组中的相应对象/字典中反映该更新。此外,如果您有一个customuitableviewcell,重置星号可能是一个好主意在“prepareForUse”函数中使用,这样当重新使用单元格时,它不会使用旧数据。

在您的评论中,您说您有一个具有选定速率的数组。但您在代码中没有显示这一点


在我看来,您也需要记录indexath,因为indexath.row与您的费率数据绑定(可能是等级?)。这样做的最佳方法是@Jay所描述的。您不应该在视图控制器中编写配置单元格数据和单元格逻辑的代码。如果您的业务逻辑很复杂,您会发现这是一场噩梦。^=^

您需要记住您对哪些人进行了评级。因为单元格是可重用对象,所以
将可重用的CellWithIdentifie退出队列r
可能会为不同的indexPath值返回同一单元格实例。您应该在
RatingTableViewCell
类中拥有评级单元格的所有逻辑。它不属于
cellForRowAtIndexPath
函数。我有一个具有选定速率的数组。我正在cellForRowAtIndexPath上使用它。我有一个模型不是用户[indexPath.row]和我在我的ratingArr中查找userId==model.id的用户。我听说使用标记保留当前索引是个坏主意。也许这就是问题所在。问题出在indexPath.row值上。当我向下滚动时,它会从1正确打印到10(我的列表有10个用户)。如果我慢慢向上滚动,它会显示数字2,但应该是4。我不知道为什么。