gpt4 book ai didi

uitableview - 为什么事件指示器显示两次

转载 作者:行者123 更新时间:2023-11-30 10:17:50 24 4
gpt4 key购买 nike

我正在实现一个事件指示器,以在获取/加载图像时显示。但是,事件指示器有时会在同一帧中显示两次。

我多次检查了代码,甚至尝试了其他方法,例如与行号匹配的计数器。知道为什么这会出现两次吗? (见下图)

事件指示器代码(在 cellForRowAtIndexPath 内):

 // start indicator when loading images
var indicatorPhoto: MaterialActivityIndicatorView! = MaterialActivityIndicatorView(style: .Small)
indicatorPhoto.center = cell.mainRestaurantImageView.center
cell.mainRestaurantImageView.addSubview(indicatorPhoto)
indicatorPhoto!.startAnimating()

cell.mainRestaurantImageView.loadInBackground {
(success: UIImage!, error: NSError!) -> Void in
if ((success) != nil) {
// stop indicator when loading images
if indicatorPhoto?.isAnimating == true {
indicatorPhoto!.stopAnimating()
indicatorPhoto!.removeFromSuperview()
}
} else {
println("Unsuccessful Fetch Image")
if indicatorPhoto?.isAnimating == true {
indicatorPhoto!.stopAnimating()
indicatorPhoto!.removeFromSuperview()
}
}
}

enter image description here

更新:

这是 cellForRowAtIndexPath 代码的其余部分

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("RestaurantCell", forIndexPath: indexPath) as FeedCell

cell.nameLabel.text = restaurantNames[indexPath.row]
cell.photoNameLabel.text = photoNames[indexPath.row]
cell.cityLabel.text = " " + addressCity[indexPath.row]
cell.distanceLabel?.text = arrayRoundedDistances[indexPath.row] + "mi"

// check if there are images
if foodPhotoObjects.isEmpty { } else {
var restaurantArrayData = self.foodPhotoObjects[indexPath.row] as PFObject
cell.mainRestaurantImageView.image = UIImage(named: "") // set placeholder
cell.mainRestaurantImageView.file = restaurantArrayData["SmPhotoUploaded"] as PFFile

// start indicator when loading images
var indicatorPhoto: MaterialActivityIndicatorView! = MaterialActivityIndicatorView(style: .Small)
indicatorPhoto.center = cell.mainRestaurantImageView.center
cell.mainRestaurantImageView.addSubview(indicatorPhoto)
indicatorPhoto!.startAnimating()

cell.mainRestaurantImageView.loadInBackground {
(success: UIImage!, error: NSError!) -> Void in
if ((success) != nil) {
// stop indicator when loading images
if indicatorPhoto?.isAnimating == true {
indicatorPhoto!.stopAnimating()
indicatorPhoto!.removeFromSuperview()
}
} else {
println("Unsuccessful Fetch Image")
if indicatorPhoto?.isAnimating == true {
indicatorPhoto!.stopAnimating()
indicatorPhoto!.removeFromSuperview()
}
}
}

cell.mainRestaurantImageView.contentMode = .ScaleAspectFill
cell.mainRestaurantImageView.clipsToBounds = true

}

return cell
}

更新2

// create indicator when loading images
var indicatorPhoto : MaterialActivityIndicatorView? = cell.mainRestaurantImageView.viewWithTag(123) as? MaterialActivityIndicatorView;
if indicatorPhoto == nil{
indicatorPhoto = MaterialActivityIndicatorView(style: .Small)
indicatorPhoto!.center = cell.mainRestaurantImageView.center
indicatorPhoto!.tag = 123
cell.mainRestaurantImageView.addSubview(indicatorPhoto!)
indicatorPhoto!.startAnimating()
}

最佳答案

此显示多次,因为您多次添加它。事实上,每次调用 foodPhotoObjects.isEmpty 的 else 情况时。

这是因为,你的方法的第一行:

let cell = tableView.dequeueReusableCellWithIdentifier("RestaurantCell", forIndexPath: indexPath) as FeedCell

使单元格从 TableView 中出列。出列工作如下:

  1. 它根据标识符维护一个队列。
  2. 如果队列中没有单元格,则会创建一个新单元格。
  3. 如果已有一个单元格,它会将该单元格返回给您。它将被重复使用。

因此,您所做的是,每次都将 MaterialActivityIndi​​catorView 添加到单元格中,无论之前是否添加过。

解决方案:

  1. 从 xib 将自定义 View 添加到您的单元格,并将其类设置为MaterialActivityIndi​​catorView。并在此处获取引用隐藏/显示和动画。
  2. 检查cell.mainRestaurantImageView的 subview ,看看是否已经有一个 MaterialActivityIndi​​catorView,获取其引用并做动画之类的。如果没有 MaterialActivityIndi​​catorView subview ,请创建一个 subview 并将其作为 subview 添加到 ImageView 中。为此,您将使用标签属性。

第二种方法可以这样做:

//first find the activity indication with tag 123, if its found, cast it to its proper class
var indicatorPhoto : MaterialActivityIndicatorView? = cell.mainRestaurantImageView.viewWithTag(123) as? MaterialActivityIndicatorView;
if indicatorPhoto == nil{
//seems it wasn't found as subview initialize here and add to mainRestaurantImageView with tag 123
}
//do rest of the stuff.

关于uitableview - 为什么事件指示器显示两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29200892/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com