【发布时间】:2017-01-26 02:46:46
【问题描述】:
我用朋友的联系信息制作了表格视图。
如果触摸每个单元格都有按钮,
我想将信息插入到选定的朋友数组
(通过数组,我又做了一个小视图与好友列表一起向上滑动)。
但如果再按一次按钮,
我想删除所选好友数组中的好友信息。
我知道如何追加到数组,
但我不知道如何擦除数组中的特定项目(NSObject)
通过不使用索引。
我的源代码在下面
class FriendModel : NSObject {
dynamic var index : ""
dynamic var thumbnail : ""
dynamic var name : ""
}
在视图控制器类中,
var selectedList = [FriendModel]()
@IBAction func SelectAct(_ sender: Any) {
let chooseBtn = sender as! UIButton
let indexPath = NSIndexPath(row: chooseBtn.tag, section:0)
let cell = tableView.cellForRow(at: indexPath as IndexPath) as! FriendsListSendCell
// when selected button is pushed
if chooseBtn.isSelected == true {
chooseBtn.isSelected = false
count = count! - 1
if self.count! < 1 {
self.windowShowUp = false
UIView.animate(withDuration: 0.1, delay: 0.1, options: [], animations:{self.selectedBoard.center.y += 80 }, completion: nil)
self.checkNumLabel.text = ""
}else{
}
//////////////////////////here////////////////////////////
//////////////////how to erase the FriendModel(NSObject) in selectedList.
}
//when the unselected button is pushed
else {
//instance for append the friend info
let friendInfo = FriendModel()
chooseBtn.isSelected = true
count = count! + 1
friendInfo.thumbnail = cell.thumbnail
friendInfo.name = cell.nameLabel.text!
//add friend info to selectedList
self.selectedList.append(friendInfo)
print("\(self.selectedList)")
if self.windowShowUp! == false{
self.windowShowUp = true
UIView.animate(withDuration: 0.1, delay: 0.1, options: [], animations:{self.selectedBoard.center.y -= 80 }, completion: nil)
}else{
}
}
}
【问题讨论】:
-
你能用一个具体的问题更新你的帖子吗?
-
@BenjaminLowry 我编辑了它!谢谢
-
您还应该为 FriendInfo 保存一个唯一标识符。否则您可以搜索它(来自@MirekE 的解决方案),以后不要对所选列表执行任何操作
-
要将索引保存到 FriendInfo 你应该小心,因为这可以快速改变(使用你的代码)
-
@muescha 和 OP:我想 FriendInfo.index 是某种唯一 ID,而不是数组索引。您不需要在此处存储数组索引。但是您可能应该有某种唯一标识符,以确保不会多次将同一记录输入到数组中。你也可以使用 Set 而不是 Array。
标签: arrays swift xcode nsobject