Ios 动态添加/删除koloda视图

Ios 动态添加/删除koloda视图,ios,swift,swipe,Ios,Swift,Swipe,我使用Koloda库管理屏幕上的可拖动卡片 我的数据源是UIImage的数组: private var numberOfRecommendedUsers: UInt = 5 let urlPicture = NSURL(string:"http://...") let imageData = NSData(contentsOfURL: urlPicture!) self.dataSource = { var array: Array<UIImage> = [] for _

我使用Koloda库管理屏幕上的可拖动卡片

我的数据源是UIImage的数组:

private var numberOfRecommendedUsers: UInt = 5

let urlPicture = NSURL(string:"http://...")
let imageData = NSData(contentsOfURL: urlPicture!)
self.dataSource = {
  var array: Array<UIImage> = []
  for _ in 0..<numberOfRecommendedUsers {
    array.append(UIImage(data: imageData!)!)
  }
  return array
}()
这很好,但它非常消耗内存(用户将刷数百张卡)

当刷卡时,它就不再使用了。 因此,解决方案是在添加新卡时将其从数据源中删除。因此,数据源始终只消耗内存中的5个元素

我发现的唯一方法是:

func koloda(koloda: KolodaView, didSwipeCardAtIndex index: UInt, inDirection direction: SwipeResultDirection) {
  let urlPicture = NSURL(string:"http:...")
  let imageData = NSData(contentsOfURL: urlPicture!)
  self.dataSource.insert(UIImage(data: imageData!)!, atIndex: kolodaView.countOfCards)
  self.dataSource.removeAtIndex(0)
  kolodaView.resetCurrentCardIndex()
}
func koloda(koloda: KolodaView, didSwipeCardAtIndex index: UInt, inDirection direction: SwipeResultDirection) {
  if direction == .Left {
    // get one user from DB
    getUsers(limit: 1, onCompleted: {(users) in
      for user in users! {
        if let koloUserDic = user as?  Dictionary<String, AnyObject> {
          self.koloUsers.append(KolodaUser(koloUserDic))
        }
      }
      dispatch_async(dispatch_get_main_queue()) {
        self.kolodaView.reloadData()
      }
    })
  }
}

func koloda(koloda: KolodaView, viewForCardAtIndex index: UInt) -> UIView {
  let kolodaUserView = NSBundle.mainBundle().loadNibNamed("KolodaUserView",
                                                       owner: self, options: nil)[0] as? KolodaUserView
  let user = self.koloUsers[Int(index)]
  kolodaUserView?.photoImageView?.setImageWithUrl(NSURL(string: "http://.../" + user.photoUrlString)!)
  kolodaUserView?.nameAndAgeLabel?.text = "\(user.firstname)" + ", " + "\(user.age)"

  return kolodaUserView!
}
问题是,
resetCurrentCardIndex()
call会在每次刷卡时重新加载加载动画的卡

预期的行为将是在数据源的末尾添加一张新卡,并删除第一个已经在没有任何动画的情况下刷过的元素

有办法吗?或者也许有一个更好的和适当的解决方案,我没有看到

感谢您的反馈

编辑

我刚刚找到了一种方法来删除动画时,重新加载卡。有一个委托方法要实现:

func kolodaShouldApplyAppearAnimation(koloda: KolodaView) -> Bool {
  return !self.reloadData
}
滑动委托方法变为:

func koloda(koloda: KolodaView, didSwipeCardAtIndex index: UInt, inDirection direction: SwipeResultDirection) {
  let urlPicture = NSURL(string:"http:...")
  let imageData = NSData(contentsOfURL: urlPicture!)
  self.dataSource.insert(UIImage(data: imageData!)!, atIndex: kolodaView.countOfCards)
  self.dataSource.removeAtIndex(0)
  self.reloadData = true
  kolodaView.resetCurrentCardIndex()
  self.reloadData = false
}
这是我所期望的行为,如果有人有更好或适当的解决方案,请随意提出

编辑2 我实施了詹姆斯提出的建议

我为将在koloda视图中显示的内容制作了一个类:

private var numberOfUsers: UInt = 5

class KolodaUser {
  var photoUrlString = ""
  var firstname = ""
  var age = 0
  var id = ""

  init () {
  }

  convenience init(_ dictionary: Dictionary<String, AnyObject>) {
    self.init()

    firstname = (dictionary["firstname"] as? String)!
    photoUrlString = ((dictionary["pictures"]) as? [String])![0]
    age = (dictionary["age"] as? Int)!
    id = (dictionary["id"] as? String)!
  }
}

@IBOutlet weak var kolodaView: KolodaView!
var koloUsers = Array<KolodaUser>()
private var numberOfUsers:UInt=5
类KolodaUser{
var photoUrlString=“”
var firstname=“”
变量年龄=0
var id=“”
init(){
}
便利初始化(字典:字典){
self.init()
firstname=(字典[“firstname”]作为?字符串)!
photoUrlString=((字典[“图片])为?[String])![0]
年龄=(字典[“年龄”]as?Int)!
id=(字典[“id”]作为?字符串)!
}
}
@ibvar-kolodaView:kolodaView!
var koloUsers=Array()
加载视图时,我从远程数据库获取5个用户的数据:

func fetchUsers() {
  getUsers(limit: 5, onCompleted: {(users) in
    for user in users! {
      if let koloUserDic = user as?  Dictionary<String, AnyObject> {
        self.koloUsers.append(KolodaUser(koloUserDic))
      }
    }
    dispatch_async(dispatch_get_main_queue()) {
      self.kolodaView.reloadData()
    }
  })
}
func fetchUsers(){
getUsers(限制:5,未完成:{(用户)在
对于用户中的用户{
如果让koloUserDic=用户作为字典{
self.koloUsers.append(KolodaUser(koloUserDic))
}
}
dispatch\u async(dispatch\u get\u main\u queue()){
self.kolodaView.reloadData()
}
})
}
现在,我的koloda委托方法如下:

func koloda(koloda: KolodaView, didSwipeCardAtIndex index: UInt, inDirection direction: SwipeResultDirection) {
  let urlPicture = NSURL(string:"http:...")
  let imageData = NSData(contentsOfURL: urlPicture!)
  self.dataSource.insert(UIImage(data: imageData!)!, atIndex: kolodaView.countOfCards)
  self.dataSource.removeAtIndex(0)
  kolodaView.resetCurrentCardIndex()
}
func koloda(koloda: KolodaView, didSwipeCardAtIndex index: UInt, inDirection direction: SwipeResultDirection) {
  if direction == .Left {
    // get one user from DB
    getUsers(limit: 1, onCompleted: {(users) in
      for user in users! {
        if let koloUserDic = user as?  Dictionary<String, AnyObject> {
          self.koloUsers.append(KolodaUser(koloUserDic))
        }
      }
      dispatch_async(dispatch_get_main_queue()) {
        self.kolodaView.reloadData()
      }
    })
  }
}

func koloda(koloda: KolodaView, viewForCardAtIndex index: UInt) -> UIView {
  let kolodaUserView = NSBundle.mainBundle().loadNibNamed("KolodaUserView",
                                                       owner: self, options: nil)[0] as? KolodaUserView
  let user = self.koloUsers[Int(index)]
  kolodaUserView?.photoImageView?.setImageWithUrl(NSURL(string: "http://.../" + user.photoUrlString)!)
  kolodaUserView?.nameAndAgeLabel?.text = "\(user.firstname)" + ", " + "\(user.age)"

  return kolodaUserView!
}
func-koloda(koloda:KolodaView,didswipecardatinex索引:UInt,间接方向:SwipeResultDirection){
如果方向==。左{
//从数据库中获取一个用户
getUsers(限制:1,未完成:{(用户)在中)
对于用户中的用户{
如果让koloUserDic=用户作为字典{
self.koloUsers.append(KolodaUser(koloUserDic))
}
}
dispatch\u async(dispatch\u get\u main\u queue()){
self.kolodaView.reloadData()
}
})
}
}
func koloda(koloda:KolodaView,viewForCardAtIndex:UInt)->UIView{
让kolodaUserView=NSBundle.mainBundle().loadNibNamed(“kolodaUserView”,
所有者:self,选项:nil)[0]作为?KolodaUserView
让user=self.koloUsers[Int(index)]
kolodaUserView?.photoImageView?.setImageWithUrl(NSURL)(字符串:http://.../“+user.photoUrlString)!)
kolodaUserView?.nameAndAgeLabel?.text=“\(user.firstname)”+,“+”\(user.age)”
返回kolodaUserView!
}
函数
setImageWithUrl
来自存储库

关于这个解决方案,您可以注意到,
KolodaUser
对象被无限地添加到koloda视图数据源数组中。 如果用户刷上千次,这将导致一个非常大的数组,但我认为这不会是一个内存问题。也许我错了


感谢您对此新解决方案的反馈。

您如何管理正在加载的图像的列表URL?您已经在这里有效地创建了一个图像缓存,但是由于您正在向
数据源
数组添加一个直接从中删除对象的缓存,因此您已经非常积极地管理了koloda视图。为什么不让
dataSource
成为您想要的图像的所有URL的数组,然后使用
UIImageView+AFNetworking
category的
setImageWithURL:
方法来更新卡?AFNetworking为您管理图像缓存。@詹姆斯:我刚刚根据您的评论用一种新的解决方案编辑了我的文章。你能告诉我你的感受吗?