使用UITableView,在控件庫中,拖拽一個Table View到ViewController中,在Controller的后臺代碼中需要繼承UITableViewDelegate和UITableViewDataSource的協議。
重寫方法
tableView(_:numberOfRowsInSection)
此方法是UITableViewDataSource協議的方法,返回tableView加載的行數。
實例代碼
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// Return the number of rows in the section.
//返回table中每個節點行數,在tableView中還可以使用節,就是組。后續介紹組的用法
return restaurantNames.count
}
tableView(_:cellForRowAtIndexPath)
此方法是UITableViewDatSource協議的方法,該方法中實現如何加載TableView中的數據。
實例代碼
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->UITableViewCell {
//這里是的Cell是在Storyboard中設置的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中取回一個名稱是[identifier]的可用的單元格。這里為什么是從列隊中?。嚎慈缦陆忉專?br />
兩種方式設置tableView的dataSource和delegate
將dataSource和delegate拖動到當前的ViewController上
@IBOutlet var tableView: UITableView!
然后再viewDidLoad的方法中設置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!
設置tableViewCell的class為新建的class
設置控件關聯
關聯后結果
在tableView(_:cellForRowAtIndexpath)的方法中,把原來的獲取單元格的方法修改
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath:
indexPath) as UITableViewCell
修改后
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath:
indexPath) as CustomTableViewCell
CustomTableViewCell就是我們新定義的類
設置單元格
cell.nameLabel.text = restaurantNames[indexPath.row]
cell.thumbnailImageView.image = UIImage(named: restaurantImages[indexPath.row])
代碼實現:
cell.thumbnailImageView.layer.cornerRadius = cell.thumbnailImageView.frame.size.width / 2
cell.thumbnailImageView.clipsToBounds = true
clipsToBounds是一個屬性開關,只有打開,圓角設置才有效
設置實現:
選擇ImageView控件,做如下設置
在User Defined Runtime Attributes中添加key 和 value。 這時不需要設置開關。
同樣,我們可以在這里修改backgroundColor,Fond…
在UITableViewDelegate的協議中,有兩個方法
tableView(_:willSelectRowAtIndexpath) 選擇前
tableView(_:didSelectRowAtIndexpath) 選擇后
let cell = tableView.cellForRowAtIndexPath(indexPath)
通過indexPath來獲取單元格的時候會產生一個Bug,前面我們有講過,Cell的加載是通過queue中獲取,受queue的印象,在獲取cell的時候,會有錯位的Bug,解決這個問題的方法是通過控制數據源來解決。
通過設置cell的accessoryType來設置cell的選中狀態
cell.accessoryType = UITableViewCellAccessoryType.Checkmark
實例代碼我們寫在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首先是要創建UIAlertController對象
let optionMenu = UIAlertController(title: nil, message: "What do you want to do?",
preferredStyle: .ActionSheet)
2、然后創建UIAlertAction
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
3、把action添加到Controller中,如果AlertControllerStyole是.ActionSheet,那么在AlertController中可以添加多個Action
optionMenu.addAction(cancelAction)
4、呈現AlertController
// Display the menu
self.presentViewController(optionMenu, animated: true, completion: nil)
在創建action中,有關handler,這里是個委托,可以用閉包實現,也可以做個函數傳遞函數名稱,就是在action點擊后觸發的委托
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
})
簡化閉包寫法
let isVisitedAction = UIAlertAction(title: "I've beenhere", style: .Default) {
//$0表示第一個參數,這里關閉閉包用法可以參考語法筆記
let sender = $0
let cell = tableView.cellForRowAtIndexPath(indexPath)
cell?.accessoryType = .Checkmark
self.restaurantIsVisited[indexPath.row] = true
}
UIAlertView類似UIAlertController中的Action,使用相對簡單
let alertView = UIAlertView(title:"", message:"", delegate:nil, cancelButtonTitle:"")
alertView.show()
在UITableViewDataSource的協議中有個方法
tableView(_:commitEditingStyle:forRowAtIndexPath
在ViewController的類中重寫該方法,不做任何實現可以看到如下效果
override func tableView(tableView: UITableView,
commitEditingStyle editingStyle:UITableViewCellEditingStyle,
forRowAtIndexPath indexPath: NSIndexPath) {
}
當點擊刪除按鈕,如果要做相關操作,可以在上述方法中實現
實例代碼:
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中有個新成員UITableViewRowAction,利用這個新成員,我們可以在Row上面有更多的操作
var shareAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title:
"Share", handler:nil)
實例代碼:
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]
}
新聞熱點
疑難解答