【问题标题】:Swift facebook taggable_friends in uitable viewSwift facebook 在 uitableview 中标记了朋友
【发布时间】:2018-07-01 00:37:38
【问题描述】:

我一直在尝试加载 facebook taggable_friends,我能够获取它们但无法在 UItableView 中加载它们。我尝试在方法“cell for Row at”中获取请求,但它会更改顺序或每 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]()

【问题讨论】:

    标签: ios swift facebook api uitableview


    【解决方案1】:

    问题是您使用两个数组而不是两个数组使用一个字典数组,例如:[[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"]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多