麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

BeginningiOS8ProgrammingwithSwift-TableView

2019-11-14 19:52:36
字體:
供稿:網(wǎng)友

UITableView控件使用

使用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è)置dataSource和delegate

兩種方式設(shè)置tableView的dataSource和delegate

  1. 在Storyboard中右鍵選擇tableView


將dataSource和delegate拖動(dòng)到當(dāng)前的ViewController上

  1. 在Controller的代碼中,鏈接tableView

@IBOutlet var tableView: UITableView!
然后再viewDidLoad的方法中設(shè)置tableView的dataSource和delegate
tableView.dataSource = self
tableView.delegate = self

設(shè)置單元格的縮略圖

在tableView(_:cellForRowAtIndexpath)的方法中
cell.imageView.image = UIImage(name:"imagename")

隱藏狀態(tài)欄

override func PRefersstatusBarHidden() -> Bool {
return true
}

自定義單元格

修改Custom屬性

在storyboard中選擇tableViewCell,在屬性索引器中,將Style的屬性變更為Custom

修改tableView行高

修改單元格行高

自定義單元格樣式

在控件庫中,可以拖拽控件到單元格中拜訪出自己想要的格式

為自定義單元格創(chuàng)建類文件

在工程中添加新文件(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!

關(guān)聯(lián)類文件和控件定義

設(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è)置圖片圓角

代碼實(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…

單元格選擇和UIAlertController

在UITableViewDelegate的協(xié)議中,有兩個(gè)方法
tableView(_:willSelectRowAtIndexpath) 選擇前
tableView(_:didSelectRowAtIndexpath) 選擇后

設(shè)置單元格選中

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

UIAlertController的使用

實(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

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

TableRow的刪除

在UITableViewDataSource的協(xié)議中有個(gè)方法
tableView(_:commitEditingStyle:forRowAtIndexPath(smile)
在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() 
}

添加RowAction

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

  1. 重寫UITableViewDataSource的一個(gè)方法tableView(_:editActionsForRowAtIndexPath)
  2. 創(chuàng)建UITableViewRowAction

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

  1. 實(shí)現(xiàn)Action后的委托
  2. 返回RowAction

實(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]
}


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: a黄在线观看 | 精品一区二区三区中文字幕老牛 | 亚洲成年人免费网站 | 狠狠色噜噜狠狠狠米奇9999 | 国产麻豆交换夫妇 | www.91pron| 国产99久久久久久免费看农村 | 欧美成人免费 | 欧美无极品 | 99在线热播精品免费 | 中文字幕免费看 | 欧美国产一级片 | 蜜桃久久一区二区三区 | 国产成人精品自拍视频 | 国产精品99久久久久久久 | 亚洲一区二区三区视频免费 | 亚洲欧美日韩精品久久 | 97久久人人超碰caoprom | 日日碰日日操 | 4399一级成人毛片 | a网站在线| 夜间福利视频 | 久久精品女人天堂av | 日产精品一区二区三区在线观看 | 成人在线观看免费观看 | 欧美日韩高清一区二区三区 | 精品国产91久久久久 | 亚洲成人精品国产 | 中文字幕亚洲一区二区三区 | 一区二区三区日本在线观看 | 欧美日韩亚洲不卡 | 91在线看黄 | 圆产精品久久久久久久久久久 | 一级做受毛片免费大片 | 黄色大片免费网站 | 久久久国产一级片 | 欧美日韩高清不卡 | 久久久久免费精品国产小说色大师 | 国产精品一区在线看 | 毛片在哪看 | 极品美女一级毛片 |