上一话我们使用了自己定义的控制器之后发现tableview上的餐馆没有了,这一话我们来添加数据,新添加一个餐馆类,这个类我们不需要继承系统的类,直接添加一个swift文件就好
import Foundation
class Restaurant {
var name = ""
var location = ""
var score = 10
}然后我们回到餐馆排行的控制器中新建一个餐馆的数组。
var restaurantList = [Restaurant]()
func loadInitData(){
let rest1 = Restaurant()
rest1.name = "蜀渝蜀二"
restaurantList.append(rest1)
let rest2 = Restaurant()
rest2.name = "芳芳餐厅"
restaurantList.append(rest2)
let rest3 = Restaurant()
rest3.name = "阿慧餐厅"
restaurantList.append(rest3)
let rest4 = Restaurant()
rest4.name = "酱府老碗面"
restaurantList.append(rest4)
let rest5 = Restaurant()
rest5.name = "要德平价川菜"
restaurantList.append(rest5)
let rest6 = Restaurant()
rest6.name = "大碗饭"
restaurantList.append(rest6)
} override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return 1
}这个方法规定了表格的区块,有的表格比较复杂可能不止一个区域,我们返回1代表只有一个区块。
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
return restaurantList.count
}这个是选中后会有什么颜色,我们选择None。回到代码中:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("PCell", forIndexPath: indexPath) as UITableViewCell
// Configure the cell...
return cell
}cell.textLabel?.text = restaurantList[indexPath.row].name//行数运行效果:
原文:http://blog.csdn.net/cg1991130/article/details/43940463