首页 > 关于IOS 开发中 Json 数据 展示到 TableView 中的思路.

关于IOS 开发中 Json 数据 展示到 TableView 中的思路.

Json 如下

Optional({
    address = "";
    affectivestatus = "";
    alipay = "";
    bio = "";
    birthcity = "";
    birthcommunity = "";
    birthdist = "";
    birthprovince = "";
    company = 11;
    education = "";
    gender = 0;
    github = 87875;
    graduateschool = "";
    idcard = "";
    idcardtype = "";
    lookingfor = "";
    nationality = "";
    occupation = "";
    position = "";
    qq = "";
    realname = "";
    residecity = "";
    residecommunity = "";
    residedist = "";
    resideprovince = "";
    residesuite = "";
    revenue = "";
    site = "";
    telephone = "";
    uid = 1;
    weixin = "";
    zipcode = "";
})

需要展示成这样

import UIKit
import Alamofire


class WoGeenziliaoViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {

    //用户头像
    @IBOutlet weak var woPortrait: UIImageView!
    //用户名称
    @IBOutlet weak var woName: UILabel!
    //用户资料列表
    @IBOutlet weak var woList: UITableView!
    
    var abc = NSDictionary()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        woList.delegate = self
        woList.dataSource = self
        
        woPortrait?.layer.cornerRadius = 50
        woPortrait?.layer.masksToBounds = true
        woPortrait.image = UIImage(named: "portrait")
        
        woList.separatorColor = UIColor(red: 239/255, green: 239/255, blue: 244/255, alpha: 1.0)
        // Do any additional setup after loading the view.

        
    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        //println(self.abc.count)
        return self.abc.count
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cellid = "FujinID"
        var index = indexPath.row
        
        var cell:Wo_GerenziliaoTableViewCell? = woList.dequeueReusableCellWithIdentifier(cellid) as? Wo_GerenziliaoTableViewCell
        if(cell == nil){
            cell = Wo_GerenziliaoTableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: cellid)
        }
        cell?.Project?.text = "555"
        cell?.Value!.text = "123"
        Alamofire.request(.GET, "http://localhost/yuanyuan_web/index.php?m=api&c=User&a=getUserDetailsData", parameters: ["uid":1]).responseJSON() { (_, _, jsonData, _) -> Void in
            println(jsonData)
            if var j = jsonData as? NSDictionary{
                self.abc = j
                //昵称
                var username = j.valueForKey("username") as? String
                //荣誉
                // var
                //猿类
                var yuantype = j.valueForKey("yuantype") as? NSString
                //个人签名
                var bio = j.valueForKey("yuantype") as? NSString
                //公司
                var company = j.valueForKey("company") as? NSString
                //地区
                var residecity = j.valueForKey("residecity") as? NSString
                //主页/博客
                var site = j.valueForKey("site") as? NSString
                //GitHub
                var github = j.valueForKey("github") as? NSString
                
            }
            
        }
     //   cell?.Project?.text = FaxianListC[index]["Project"]
     //   cell?.Value!.text = FaxianListC[index]["Value"]
        
        return cell!
    }
    
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat{
        return 40.0
    }


}

你放在cellForRowAtIndexPath里面写,然后你知道要请求多少次么,self.abc这个数组里面是空的,Swift的机制应该是要强制初始化,self.abc这个数组要初始化,如果我没有猜错的话,你这个一运行就崩了,因为方法numberOfRowsInSection返回的是0


看一下这门教学课程,尤其是最后三个视频,告诉了你先根据要填入的数据声明一个结构体,并生成一个可以在结构体的数据didSet时后重新加载配置视图程序(reloadData)的实例。并在最后实现赋值从而调用didSet。


写过swift,但是我要说的和@isteven 一样,网络请求不应该放到cellForRowAtIndexPath方法中,这并不是一种优雅的做法,虽然你使用Alamofire这个异步网络请求框架,这么做,我们说没有错误,只是不够优雅,优雅的方式是,在viewDidLoad 中触发网络请求,Alamofire请求拿到json数据之后在在数据塞给tableview,最后让tableview刷新。


虽然没有写过Swift,但应该在viewDidLoad之类的里面做网络请求,然后请求到得Json存放到一个NSArray内,array内对象是字符串。然后cellForRowAtIndexPath方法里,只要array[indexPath.row]这样取数据。


第一,我强烈建议将这个页面的信息写成一个pac,然后在cell里面直接调用。第二,网络请求最好写在viewDidLoad里面。第三,将网络请求单独写成method进行快速调用。另外,我还建议你使用网络请求的delegate,在请求成功后,在delegate里面写出信息更新的方法。


虽然没写过Swift的,但是,请求网络的代码不应该放到cellForRowAtIndexPath方法中,因为它会被调用很多次!
应该在viewDidLoad的时候请求网络,网络回调时将解析后的json保存起来,然后调用UITableView的reloadData方法刷新UITableView。
在cellForRowAtIndexPath中,使用刚刚保存的解析后的json里的数据来设置每一个cell。

【热门文章】
【热门文章】