iOS UITableView滚动时列表项内容与图片错乱问题求助(已解决Android端同类问题)
iOS UITableView滚动时列表项内容与图片错乱问题求助(已解决Android端同类问题)
我同时开发Android(Java)和iOS(Swift)应用,之前Android端遇到列表滚动时项的内容、图片串位的问题,重写下面两个方法就完美解决了:
@Override public long getItemId(int position) { return position; } @Override public int getItemViewType(int position) { return position; }
但现在iOS端用UITableView做的列表,滚动时遇到了完全一样的问题——有时候滑着滑着,列表项里的标题、描述甚至图片就莫名其妙串到别的行去了!我翻遍了UITableView的代理和数据源方法,没找到能对应解决这个问题的等效重写逻辑,想请教下各位iOS大佬该怎么处理?
先给大家说下我的列表结构:就是一个很简单的列表,每个列表项左侧是一张图片,右侧是标题和描述文字,样式很常规。
我怀疑会不会是图片加载的问题?目前我是这么实现图片加载和cell配置的:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "mainCell", for: indexPath) as! MainTableCell let item = radio_type_list[indexPath.row] cell.RadioTitle.text = item.title cell.RadioDescription.text = item.desc cell.RadioImage.downloadImageFrom(link:item.imageurl, contentMode: UIView.ContentMode.scaleToFill) return cell; } let imageCache = NSCache<NSString, AnyObject>() extension UIImageView { func downloadImageFrom(link:String, contentMode: UIView.ContentMode) { let imageCache = NSCache<NSString, AnyObject>() if let cachedImage = imageCache.object(forKey: (link as AnyObject) as! NSString) { self.image = cachedImage as? UIImage self.contentMode = .scaleAspectFill return } URLSession.shared.dataTask( with: NSURL(string:link)! as URL, completionHandler: { (data, response, error) -> Void in DispatchQueue.main.async { self.contentMode = contentMode if let data = data { self.image = UIImage(data: data) } } }).resume() } }
我搜了UITableView的重写方法,尝试找类似Android端getItemId和getItemViewType的等效逻辑,但一直没找到能解决我问题的方法,有没有大佬能给点思路?
备注:内容来源于stack exchange,提问作者yhackup
相关产品推荐
相关产品推荐

