使用UITableView,在控件庫中,拖拽一個(gè)Table View到ViewController中,在Controller的后臺(tái)代碼中需要繼承UITableViewDelegate和UITableViewDataSource的協(xié)議。
重寫方法
tableView(_:numberOfRowsInSection)
此方法是UITableViewDataSource協(xié)議的方法,返回tableView加載的行數(shù)。
實(shí)例代碼
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// Return the number of rows in the section.
//返回table中每個(gè)節(jié)點(diǎn)行數(shù),在tableView中還可以使用節(jié),就是組。后續(xù)介紹組的用法
return restaurantNames.count
}
tableView(_:cellForRowAtIndexPath)
此方法是UITableViewDatSource協(xié)議的方法,該方法中實(shí)現(xiàn)如何加載TableView中的數(shù)據(jù)。
實(shí)例代碼
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->UITableViewCell {
//這里是的Cell是在Storyboard中設(shè)置的tableViewCell的Identifier
let cellIdentifier = "Cell"
//這就是獲取單元格
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath:
indexPath) as UITableViewCell
// Configure the cell...
cell.textLabel.text = restaurantNames[indexPath.row]
return cell
}
dequeueReusableCellWithIdentifier:從queue中取回一個(gè)名稱是[identifier]的可用的單元格。這里為什么是從列隊(duì)中取:看如下解釋:
兩種方式設(shè)置tableView的dataSource和delegate
將dataSource和delegate拖動(dòng)到當(dāng)前的ViewController上
@IBOutlet var tableView: UITableView!
然后再viewDidLoad的方法中設(shè)置tableView的dataSource和delegate
tableView.dataSource = self
tableView.delegate = self
在tableView(_:cellForRowAtIndexpath)的方法中
cell.imageView.image = UIImage(name:"imagename")
override func PRefersstatusBarHidden() -> Bool {
return true
}
在storyboard中選擇tableViewCell,在屬性索引器中,將Style的屬性變更為Custom
修改tableView行高
修改單元格行高
在控件庫中,可以拖拽控件到單元格中拜訪出自己想要的格式
在工程中添加新文件(command + N),選擇Cocoa Touch Class,在SubClass of中選擇UITableViewCell,不需要xib文件。
在類文件中定義控件
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var locationLabel: UILabel!
@IBOutlet weak var typeLabel: UILabel!
@IBOutlet weak var thumbnailImageView: UIImageView!
設(shè)置tableViewCell的class為新建的class
設(shè)置控件關(guān)聯(lián)
關(guān)聯(lián)后結(jié)果
在tableView(_:cellForRowAtIndexpath)的方法中,把原來的獲取單元格的方法修改
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath:
indexPath) as UITableViewCell
修改后
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath:
indexPath) as CustomTableViewCell
CustomTableViewCell就是我們新定義的類
設(shè)置單元格
cell.nameLabel.text = restaurantNames[indexPath.row]
cell.thumbnailImageView.image = UIImage(named: restaurantImages[indexPath.row])
代碼實(shí)現(xiàn):
cell.thumbnailImageView.layer.cornerRadius = cell.thumbnailImageView.frame.size.width / 2
cell.thumbnailImageView.clipsToBounds = true
clipsToBounds是一個(gè)屬性開關(guān),只有打開,圓角設(shè)置才有效
設(shè)置實(shí)現(xiàn):
選擇ImageView控件,做如下設(shè)置
在User Defined Runtime Attributes中添加key 和 value。 這時(shí)不需要設(shè)置開關(guān)。
同樣,我們可以在這里修改backgroundColor,F(xiàn)ond…
在UITableViewDelegate的協(xié)議中,有兩個(gè)方法
tableView(_:willSelectRowAtIndexpath) 選擇前
tableView(_:didSelectRowAtIndexpath) 選擇后
let cell = tableView.cellForRowAtIndexPath(indexPath)
通過indexPath來獲取單元格的時(shí)候會(huì)產(chǎn)生一個(gè)Bug,前面我們有講過,Cell的加載是通過queue中獲取,受queue的印象,在獲取cell的時(shí)候,會(huì)有錯(cuò)位的Bug,解決這個(gè)問題的方法是通過控制數(shù)據(jù)源來解決。
通過設(shè)置cell的accessoryType來設(shè)置cell的選中狀態(tài)
cell.accessoryType = UITableViewCellAccessoryType.Checkmark
實(shí)例代碼我們寫在tableView(_:didSelectRowAtIndexpath)的方法中
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath:
NSIndexPath) {
// Create an option menu as an action sheet
let optionMenu = UIAlertController(title: nil, message: "What do you want to do?",
preferredStyle: .ActionSheet)
// Add actions to the menu
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
optionMenu.addAction(cancelAction)
// Display the menu
self.presentViewController(optionMenu, animated: true, completion: nil)
}
UIAlertControllerStyle有兩種,
.Alert
.ActionSheet
1、使用AlertController首先是要?jiǎng)?chuàng)建UIAlertController對(duì)象
let optionMenu = UIAlertController(title: nil, message: "What do you want to do?",
preferredStyle: .ActionSheet)
2、然后創(chuàng)建UIAlertAction
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
3、把a(bǔ)ction添加到Controller中,如果AlertControllerStyole是.ActionSheet,那么在AlertController中可以添加多個(gè)Action
optionMenu.addAction(cancelAction)
4、呈現(xiàn)AlertController
// Display the menu
self.presentViewController(optionMenu, animated: true, completion: nil)
在創(chuàng)建action中,有關(guān)handler,這里是個(gè)委托,可以用閉包實(shí)現(xiàn),也可以做個(gè)函數(shù)傳遞函數(shù)名稱,就是在action點(diǎn)擊后觸發(fā)的委托
let isVisitedAction = UIAlertAction(title: "I've beenhere", style: .Default, handler: {
(action:UIAlertAction!) -> Void in
let cell = tableView.cellForRowAtIndexPath(indexPath)
cell?.accessoryType = .Checkmark
self.restaurantIsVisited[indexPath.row] = true
})
簡(jiǎn)化閉包寫法
let isVisitedAction = UIAlertAction(title: "I've beenhere", style: .Default) {
//$0表示第一個(gè)參數(shù),這里關(guān)閉閉包用法可以參考語法筆記
let sender = $0
let cell = tableView.cellForRowAtIndexPath(indexPath)
cell?.accessoryType = .Checkmark
self.restaurantIsVisited[indexPath.row] = true
}
UIAlertView類似UIAlertController中的Action,使用相對(duì)簡(jiǎn)單
let alertView = UIAlertView(title:"", message:"", delegate:nil, cancelButtonTitle:"")
alertView.show()
在UITableViewDataSource的協(xié)議中有個(gè)方法
tableView(_:commitEditingStyle:forRowAtIndexPath
在ViewController的類中重寫該方法,不做任何實(shí)現(xiàn)可以看到如下效果
override func tableView(tableView: UITableView,
commitEditingStyle editingStyle:UITableViewCellEditingStyle,
forRowAtIndexPath indexPath: NSIndexPath) {
}
當(dāng)點(diǎn)擊刪除按鈕,如果要做相關(guān)操作,可以在上述方法中實(shí)現(xiàn)
實(shí)例代碼:
override func tableView(tableView: UITableView, commitEditingStyle editingStyle:
UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
// Delete the row from the data source
self.restaurantNames.removeAtIndex(indexPath.row)
self.restaurantLocations.removeAtIndex(indexPath.row)
self.restaurantTypes.removeAtIndex(indexPath.row)
self.restaurantIsVisited.removeAtIndex(indexPath.row)
self.restaurantImages.removeAtIndex(indexPath.row)
//刪除row
self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
}
//重新加載tableView
self.tableView.reloadData()
}
在IOS 8 SDK中有個(gè)新成員UITableViewRowAction,利用這個(gè)新成員,我們可以在Row上面有更多的操作
var shareAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title:
"Share", handler:nil)
實(shí)例代碼:
override func tableView(tableView: UITableView,
editActionsForRowAtIndexPath indexPath:
NSIndexPath) -> [AnyObject] {
var shareAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title:
"Share", handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in
let shareMenu = UIAlertController(title: nil, message: "Share using",
preferredStyle: .ActionSheet)
let twitterAction = UIAlertAction(title: "Twitter", style:
UIAlertActionStyle.Default, handler: nil)
let facebookAction = UIAlertAction(title: "Facebook", style:
UIAlertActionStyle.Default, handler: nil)
let emailAction = UIAlertAction(title: "Email", style: UIAlertActionStyle.Default,
handler: nil)
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel,
handler: nil)
shareMenu.addAction(twitterAction)
shareMenu.addAction(facebookAction)
shareMenu.addAction(emailAction)
shareMenu.addAction(cancelAction)
self.presentViewController(shareMenu, animated: true, completion: nil)
}
)
var deleteAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default,
title: "Delete",handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in
// Delete the row from the data source
self.restaurantNames.removeAtIndex(indexPath.row)
self.restaurantLocations.removeAtIndex(indexPath.row)
self.restaurantTypes.removeAtIndex(indexPath.row)
self.restaurantIsVisited.removeAtIndex(indexPath.row)
self.restaurantImages.removeAtIndex(indexPath.row)
self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
}
)
return [deleteAction, shareAction]
}
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注