我遇到一个数组的问题,它在一个单独的函数中正确填充,问题是当我尝试在我的tableview中填写我的单元格的元素时,我只能找到最后一个元素但是当我想要在填充单元格时显示该数组中的元素数量,它显示正确数量的元素,任何人都可以帮助。 这是我检索和填充数组的函数:
func downloadUserDetails(completed: @escaping DownloadComplete){
let Ful_Url = "http://192.168.1.4:8888/phps/select.php"
Alamofire.request(Ful_Url).responseJSON(completionHandler: { (response) in
if let userDect = response.result.value as? [Dictionary<String,AnyObject>]{
for ex in 0...userDect.count-1
{
if let firstnames = userDect[ex]["firstname"] as? String{
self.users?.firstname = firstnames}
if let emails = userDect[ex]["email"] as? String{
self.users?.email = emails}
if let lastnames = userDect[ex]["lastname"] as? String{
self.users?.lastname = lastnames}
print("---------------------------------")
self.items.append(self.users!)
// self.items.insert(self.users!, at: self.i)
print(self.items[ex].email)
print(self.items.count)
}
}
completed()
self.tableview.reloadData()
})
}
这就是我试图填充单元格标签的方式:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print("####################")
print("nombre items")
print(self.items.count)
return items.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:UITableViewCell = tableView.dequeueReusableCell(withIdentifier:"myCell" , for:indexPath)
let email:UILabel = cell.viewWithTag(11) as! UILabel
let firstname:UILabel = cell.viewWithTag(12) as! UILabel
let lastname:UILabel = cell.viewWithTag(13) as! UILabel
print("=========================email=========================")
print(items[indexPath.row].email)
email.text = items[indexPath.row].email
firstname.text = items[indexPath.row].firstname
lastname.text = items[indexPath.row].lastname
return cell
}
答案 0 :(得分:0)
我认为插入方法有问题:
self.item.insert(self.users!, at:self,i);
您可以尝试:
self.item.insert(self.users!, at:ex);
答案 1 :(得分:0)
我认为您的问题是您使用的是单个用户实例,然后将其附加到数组中。数组中的每个项目都指向同一个项目(类通过引用传递)。
您不需要这样做,您也不需要在迭代期间维护计数或索引。
此代码应该可以正常工作..
if let usersDict = response.result.value as? [Dictionary<String,AnyObject>] {
self.users = usersDict.map({ dict in
let user = User()
if let firstnames = dict["firstname"] as? String{
user.firstname = firstnames }
if let emails = dict["email"] as? String{
user.email = emails }
if let lastnames = dict["lastname"] as? String{
user.lastname = lastnames }
return user
})
self.tableView.reloadData()
}
或者甚至更好,允许您的User对象使用字典进行初始化,然后执行
if let usersDict = response.result.value as? [Dictionary<String,AnyObject>] {
self.users = usersDict.map({ User($0) })
self.tableView.reloadData()
}
在循环期间只使用局部变量,这里不需要类属性。要使用底部的,您需要能够使用字典初始化User对象。与此方法类似:
struct User
{
var firstName:String
var lastName:String
var email:String
init(dict:Dictionary<String,AnyObject>) {
email = dict["email"] as? String
firstName = dict["firstName"] as! String
lastName = dict["lastName"] as! String
}
}
更新:
我刚刚在一个运作良好的操场上写了这个
class User {
var firstName: String!
var lastName: String!
var email: String!
init(dict: [String:AnyObject]) {
self.firstName = dict["firstName"] as! String
self.lastName = dict["lastName"] as! String
self.email = dict["email"] as! String
}
}
let usersDict: [[String:String]] = [
["firstName": "John", "lastName": "Smith", "email": "john@example.com"],
["firstName": "John", "lastName": "Smithy", "email": "john@example.com"],
["firstName": "John", "lastName": "Stevens", "email": "john@example.com"],
["firstName": "John", "lastName": "Smithen", "email": "john@example.com"]
]
let users = usersDict.map({ User(dict: $0 as! [String : AnyObject]) })
for user in users {
print(user.firstName, user.lastName)
}
输出:
John Smith
John Smithy
John Stevens
John Smithen
答案 2 :(得分:0)
实际上我刚刚为任何一个面临同样问题的人找到了解决方案,实际上很简单,Class User的声明应该在循环内部,而不是作为类变量,所以现在我创建了一个新用户找到每个元素,然后将旧元素添加到数组中。