致命错误索引超出范围 ( Swift 2.3 )
Fatal Error Index Out Of Range ( Swift 2.3 )
当我 运行 我的项目时,我得到 Fatal error: Index Out Of Range
但我找不到解决方案。
非常感谢您的帮助,提前致谢!
这是我的代码:
import UIKit
// MARK : Data
var names = ["Shalvata",
"Markid",
"Litzman Bar",
"The Cat & The Dog",
"Light house",
"Ku"]
var streets = ["האנגר 28,נמל תל אביב",
"אבן גבירול 30,תל אביב",
"רחוב נמל תל אביב",
"קרליבך 28,תל אביב",
"האנגר 23,נמל תל אביב",
"דרך שלמה 117,תל אביב"]
var images = [UIImage(named: "Shalvata"),
UIImage(named: "Markid"),
UIImage(named: "Litzman Bar"),
UIImage(named: "CatNDog"),
UIImage(named: "LightHouse"),
UIImage(named: "Ku")]
/////////MARK : END Of Data ! ///////////
override func viewDidLoad() {
super.viewDidLoad()
allUsers = createUsers(names: names, streets: streets, images: images)
filteredUsers = allUsers
}
var allUsers: [User]!
var filteredUsers: [User]!
func createUsers(names names: [String], streets: [String], images: [UIImage?]) -> [User] {
var users = [User]()
guard names.count == streets.count && names.count == images.count else { return users }
for (index, name) in names.enumerate() {
let user = User(name: name, streetName: streets[index], image: images[index])
users.append(user)
}
return users
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//Search:
if tableView == self.tableView {
return self.names.count
} else {
return self.filteredUsers.count
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell = self.tableView.dequeueReusableCellWithIdentifier("CustomCell", forIndexPath: indexPath) as! CustomCell
let user = filteredUsers[indexPath.row]
cell.photo.image = user.image
cell.name.text = user.name
cell.streetName.text = user.streetName
return cell
}
}
我得到以下行的错误:
let user = filteredUsers[indexPath.row]
错误行:
Thread 1: EXC_BREAKPOINT(code=1, subcode=0x10033ddc0)
如果还有什么需要说的,我用的是Xcode 8和Swift 2.3,
我最近开始编码,所以我是初学者,对此感到抱歉;P
因为两个数组可以同时拥有不同数量的元素。所以你需要在下面的方法中添加一个条件来访问基于 tableview 的用户。
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell = self.tableView.dequeueReusableCellWithIdentifier("CustomCell", forIndexPath: indexPath) as! CustomCell
let user:User!
if tableView == self.tableView {
user = allUsers[indexPath.row]
} else {
user = filteredUsers[indexPath.row]
}
cell.photo.image = user.image
cell.name.text = user.name
cell.streetName.text = user.streetName
return cell
}
当我 运行 我的项目时,我得到 Fatal error: Index Out Of Range
但我找不到解决方案。
非常感谢您的帮助,提前致谢!
这是我的代码:
import UIKit
// MARK : Data
var names = ["Shalvata",
"Markid",
"Litzman Bar",
"The Cat & The Dog",
"Light house",
"Ku"]
var streets = ["האנגר 28,נמל תל אביב",
"אבן גבירול 30,תל אביב",
"רחוב נמל תל אביב",
"קרליבך 28,תל אביב",
"האנגר 23,נמל תל אביב",
"דרך שלמה 117,תל אביב"]
var images = [UIImage(named: "Shalvata"),
UIImage(named: "Markid"),
UIImage(named: "Litzman Bar"),
UIImage(named: "CatNDog"),
UIImage(named: "LightHouse"),
UIImage(named: "Ku")]
/////////MARK : END Of Data ! ///////////
override func viewDidLoad() {
super.viewDidLoad()
allUsers = createUsers(names: names, streets: streets, images: images)
filteredUsers = allUsers
}
var allUsers: [User]!
var filteredUsers: [User]!
func createUsers(names names: [String], streets: [String], images: [UIImage?]) -> [User] {
var users = [User]()
guard names.count == streets.count && names.count == images.count else { return users }
for (index, name) in names.enumerate() {
let user = User(name: name, streetName: streets[index], image: images[index])
users.append(user)
}
return users
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//Search:
if tableView == self.tableView {
return self.names.count
} else {
return self.filteredUsers.count
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell = self.tableView.dequeueReusableCellWithIdentifier("CustomCell", forIndexPath: indexPath) as! CustomCell
let user = filteredUsers[indexPath.row]
cell.photo.image = user.image
cell.name.text = user.name
cell.streetName.text = user.streetName
return cell
}
}
我得到以下行的错误:
let user = filteredUsers[indexPath.row]
错误行:
Thread 1: EXC_BREAKPOINT(code=1, subcode=0x10033ddc0)
如果还有什么需要说的,我用的是Xcode 8和Swift 2.3, 我最近开始编码,所以我是初学者,对此感到抱歉;P
因为两个数组可以同时拥有不同数量的元素。所以你需要在下面的方法中添加一个条件来访问基于 tableview 的用户。
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell = self.tableView.dequeueReusableCellWithIdentifier("CustomCell", forIndexPath: indexPath) as! CustomCell
let user:User!
if tableView == self.tableView {
user = allUsers[indexPath.row]
} else {
user = filteredUsers[indexPath.row]
}
cell.photo.image = user.image
cell.name.text = user.name
cell.streetName.text = user.streetName
return cell
}