我一直在尝试加载facebook taggable_friends
,我能够获取它们但无法在UItableView中加载它们。我尝试在方法中获取请求"在"但它会改变顺序或每2-3秒更新一次。我尝试声明数组并存储名称和个人资料图片但我遇到了崩溃。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "friendCell", for: indexPath) as! friendstvc
// The first try
/*FBSDKGraphRequest(graphPath: "me/taggable_friends?limit=5000", parameters: nil).start { (connection, result, error) in
if error != nil
{
print(error?.localizedDescription ?? "")
cell.friendName.text = ""
return
}
let dic = result as! Dictionary<String, Any>
let data = dic["data"] as! NSArray
if let name = dic["name"]
{
print(name as! String)
}
let valuedict = data[indexPath.row] as! Dictionary<String, Any>
let id = valuedict["id"] as! String
let name = valuedict["name"] as! String
cell.friendName.text = name
cell.profpic.profileID = id
}*/
// The second try
cell.friendName.text = friendnames[indexPath.row] // crashes here
cell.profpic.image = friendpictures[indexPath.row]
return cell
}
在第二次尝试的viewDidLoad中调用以下方法
// for the second try
func loadtaggableFriends()
{
// Called in the viewDidLoad
FBSDKGraphRequest(graphPath: "me/taggable_friends?limit=5000", parameters: nil).start { (connection, result, error) in
if error != nil{
print(error?.localizedDescription ?? "")
return
}
let dic = result as! Dictionary<String, Any>
let data = dic["data"] as! NSArray
if let name = dic["name"]
{
print(name as! String)
}
print(data.count)
for i in 0...data.count - 1
{
let valuedict = data[i] as! Dictionary <String, Any>
let name = valuedict["name"] as! String
var dataurl: Data?
if let picture = valuedict["picture"] as? NSDictionary, let data = picture["data"] as? NSDictionary, let url = data["url"] as? String
{
let loadurl = URL(string: url)
dataurl = try? Data(contentsOf: loadurl!)
}
friendnames.append(name)
friendpictures.append(UIImage(data: dataurl!)!)
}
}
}
// declaring the arrays
var friendnames = [String]()
var friendpictures = [UIImage]()
答案 0 :(得分:0)
问题是你使用两个数组而不是两个数组使用一个字典数组,如:[[String:AnyObject]]。使用字典和将数据插入数组的方法是:
let dict = [String:AnyObject]() //create dictionary
dict["friendpicture"] = "" //
dict["friendnames"] = "" //enter your data
arrOfDictionaries.append(dict) //insert data into array
访问cellForRowAt方法中的数据,如下所示:
let dict = arrOfDictionaries[indexPath.item]
friendpicture = dict["friendpicture"]
friendnames = dict["friendnames"]