Beginning iOS 8 Programming with Swift-TableView

來源:互聯網
上載者:User

標籤:style   http   io   color   ar   os   使用   sp   for   

UITableView控制項使用

使用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]的可用的儲存格。這裡為什麼是從列隊中取:看如下解釋:

設定dataSource和delegate

兩種方式設定tableView的dataSource和delegate

  1. 在Storyboard中右鍵選擇tableView


將dataSource和delegate拖動到當前的ViewController上

  1. 在Controller的代碼中,連結tableView

@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
}

自訂儲存格修改Custom屬性

在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…

儲存格選擇和UIAlertController

在UITableViewDelegate的協議中,有兩個方法
tableView(_:willSelectRowAtIndexpath) 選擇前
tableView(_:didSelectRowAtIndexpath) 選擇後

設定儲存格選中

let cell = tableView.cellForRowAtIndexPath(indexPath)
通過indexPath來擷取儲存格的時候會產生一個Bug,前面我們有講過,Cell的載入是通過queue中擷取,受queue的印象,在擷取cell的時候,會有錯位的Bug,解決這個問題的方法是通過控制資料來源來解決。
通過設定cell的accessoryType來設定cell的選中狀態
cell.accessoryType = UITableViewCellAccessoryType.Checkmark

UIAlertController的使用

執行個體代碼我們寫在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

UIAlertView類似UIAlertController中的Action,使用相對簡單
let alertView = UIAlertView(title:"", message:"", delegate:nil, cancelButtonTitle:"")
alertView.show()

TableRow的刪除

在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() 
}

添加RowAction

在IOS 8 SDK中有個新成員UITableViewRowAction,利用這個新成員,我們可以在Row上面有更多的操作

  1. 重寫UITableViewDataSource的一個方法tableView(_:editActionsForRowAtIndexPath)
  2. 建立UITableViewRowAction

var shareAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title:
"Share", handler:nil)

  1. 實現Action後的委託
  2. 返回RowAction

執行個體代碼:
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]
}

Beginning iOS 8 Programming with Swift-TableView

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.