到目前为止,我有一个可以处理多个选择行的 tableView。一切正常,除非我试图获取我选择的行的数组。
这是我的 Stat.swift 类:
class Stat: Equatable {
var statName: String = ""
var statCalendar: String = ""
var statEvents : [StatEvents] = []
}
struct StatEvents {
var isSelected: Bool = false
var name: String
var dateRanges: [String]
var hours: Int
}
func == (lhs: Stat, rhs: Stat) -> Bool {
return (lhs.statEvents == rhs.statEvents)
}
这是我的 EventsViewController.swift 类:
var currentStat = Stat()
var selectedMarks = [StatEvents]()
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as UITableViewCell
cell.textLabel?.font = UIFont.systemFontOfSize(8.0)
cell.textLabel?.text = "\(currentStat.statEvents[indexPath.row].name) \(currentStat.statEvents[indexPath.row].dateRanges) horas=\(currentStat.statEvents[indexPath.row].hours)"
if currentStat.statEvents[indexPath.row].isSelected{
cell.accessoryType = .Checkmark
} else {
cell.accessoryType = .None
}
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: false)
currentStat.statEvents[indexPath.row].isSelected = !currentStat.statEvents[indexPath.row].isSelected
if (contains(selectedMarks, currentStat.statEvents[indexPath.row])) {
//To-Do: remove the object in selectedMarks
} else {
selectedMarks.append(currentStat.statEvents[indexPath.row]) //add the object to selectedMarks
}
tableView.reloadData()
}
问题出在“didSelectRowAtIndexPath”方法中。当我选择任何行时,它会将对象附加到“selectedMarks”数组中(这工作正常),但问题是当我取消选择其中一些行时,它应该删除 selectedMarks 数组中的对象。我正在尝试使用“包含”方法,但在该行出现编译错误
could not find an overload for contains that accepts the supplied arguments
我通过在 Stat 类中添加 Equatable 协议(protocol)来更新我的问题,但我再次遇到相同的错误:
could not find an overload for contains that accepts the supplied arguments
还有一个新的错误:
Command failed due to signal: Segmentation fault: 11
最佳答案
为了让 contains
方法在 Swift 2 中发挥作用,您的 StatEvents
结构应该符合 Equatable
协议(protocol)如下例所示:
struct StatEvents: Equatable
{
// ...
// implementation of your structure....
// ...
}
// Needed for conforming to the Equatable protocol
func == (lhs: StatEvents, rhs: StatEvents) -> Bool
{
// Return true if the parameters are equal to each other
}
此外,Swift 2 中没有全局 contains
函数,因此您需要调用新的数组扩展方法 contains
,在您的情况下会像这样:
selectedMarks.contains(currentStat.statEvents[indexPath.row])
同时将协议(protocol)声明添加到 StatEvents
结构中,而不是添加到 Stat
类中。您对 ==
方法的实现也不正确。如上所示,它应该检查 StatEvents
类型的两个对象之间的相等性。
关于ios - 获取uitableview的多个选定行信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31094190/