tableview 与 tableview cell

1.tableview cell:


import Foundation
import UIKit

class CjwtCell: UITableViewCell {
@IBOutlet var lb_content:UILabel!    //定义lable
var data: NSDictionary?       //定义data类型
override func layoutSubviews() {
self.lb_content.text = self.data!["question"] as? String       //获取值的函数
}

}

2.tableview:

import Foundation
import UIKit

class ChangjwtController: UIViewController   //继承
,UITableViewDataSource
,UITableViewDelegate{
@IBOutlet var quxiao:UIButton!
var dataSourse=NSMutableArray()     //有一个类型不区分name string?
@IBOutlet var tableview:UITableView!

override func viewDidLoad() {
super.viewDidLoad()
self.tableview.dataSource = self  //表示函数对这个控件起作用
self.tableview.delegate = self  //表示函数对这个控件起作用
let nib = UINib(nibName: "CjwtCell", bundle: nil)  //因为要用到其他xib 故先定义nib
self.tableview.registerNib(nib, forCellReuseIdentifier: "CjwtCell")  //这里面的CjwtCell为table view cell控件名

self.title="我的";
downloadaData()
}

@IBAction func OnBackup(sender:AnyObject){
self.dismissViewControllerAnimated(true, completion: {})
}

func downloadaData(){
let url="http://59.78.93.208:9097/CJWT"

HttpRequest.request(urlString:url){(data) -> Void in
if data == nil{
let alertView=UIAlertView()
alertView.title="温馨提示"
alertView.message="加载失败"
alertView.addButtonWithTitle("确定")
alertView.addButtonWithTitle("取消")
alertView.show()
}
else{
let itemArray = data

for item:AnyObject in itemArray!{
self.dataSourse.addObject(item)
}
self.tableview!.reloadData()

}
}
}

//UITableViewDataSource   //获取tableview组数
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {   //获取你个tableview cell里面元素个数
return self.dataSourse.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("CjwtCell", forIndexPath: indexPath) as? CjwtCell

if indexPath.row < self.dataSourse.count {  
let dataDict = self.dataSourse[indexPath.row] as! NSDictionary
cell?.data = dataDict
}


return cell!
}


}

原文地址:https://www.cnblogs.com/to-creat/p/5368796.html