tags: iOS ,TableView,使用問題
說明
這個是一個簡單的對TableView的見解弛车,僅僅包括一些基本內(nèi)容的使用齐媒。
TableView 常見于APP的各種領域各種多項可擴展性的頁面,同時可以通過TableViewCell纷跛,對tableView進行進一步的擴展喻括,使得TableView盡可能的展示更多的內(nèi)容。
TableView的使用無論對于系統(tǒng)還是用戶來說都是最優(yōu)解贫奠,因為TableView很大的程度上節(jié)省了系統(tǒng)的開銷唬血,讓TableViewCell復用,可充分的提升APP的性能唤崭,讓數(shù)據(jù)變得有序可尋拷恨。利器!讓用戶在使用APP的時候浩姥,更直觀的看到數(shù)據(jù)挑随,用著順心!
有關(guān)于xib實現(xiàn)的TableView
先新建一個關(guān)于TableView的工程勒叠,然后看需求新建一個新的頁面兜挨,或者在MainStoryBoard上拖拽一個新的UI頁面,然后直接把TableView控件新建到頁面上即可眯分。具體的拖拽就不多說了拌汇,重點是為什么需要區(qū)分于MainStoryBoard拖拽和新建File拖拽?
作為一個工程的整體性來看弊决,無疑是基于故事版(mainStoryBoard)新建比較好噪舀,因為整體看起來能知道整個項目的架構(gòu)魁淳,這樣有利于后期的維護以及新加入開發(fā)人員的二次開發(fā)。至于放在一個新的ViewController里与倡,其實也是又優(yōu)勢的界逛,比方說,你需要別人為你完成某一個頁面纺座,但是你又不想多一個人對著mainStoryBoard里面瞎搞息拜,這時候就可以讓他新建一個ViewController完成tableView的內(nèi)容,好吧,上面的都是扯淡净响,最主要的是少欺,這兩種方法使用的時候在頁面push/present是不一樣的!
這部分以后再補充馋贤,再扯下去就離題了赞别。
具體的拖拽細節(jié)就不多說了,我們來對照圖片補充一下功能吧:
可能會有些地方不對??(⊙﹏⊙)b配乓,但是多試試就知道是怎么回事的了仿滔。
至于后續(xù)還有很多的選項還會用到,但是能明顯的從那一輔助欄里看出來一些繼承關(guān)系犹芹,對于那些設置是相通的:
可以看得出堤撵,tableView是基于ScrollView的擴展,然后ScrollView是基于View的擴展羽莺。這種現(xiàn)象就好像我們在一個TableView的Cell 里面嵌入一個UIView、UIButton是一樣洞豁。
有關(guān)于純代碼寫UITableView
簡單的來說就是要new一個View盐固,然后把它add到另一個View上面。不多說丈挟,上代碼刁卜!
import UIKit
class SecViewController: UIViewController ,UITableViewDataSource,UITableViewDelegate {
var mytableView:UITableView!
override func viewDidLoad() {
super.viewDidLoad()
mytableView = UITableView.init(frame: CGRect(x:0,y:20,width:UIScreen.main.bounds.size.width,height:500))
mytableView.rowHeight = 40.0
mytableView.backgroundColor = UIColor.white
mytableView.tableFooterView = UIView.init()
self.view.addSubview(mytableView)
//接收代理
mytableView.delegate = self
mytableView.dataSource = self
// Do any additional setup after loading the view.
}
是的swift3.0版的是這樣子的
然后來點Objective-C的
@interface testTableViewController ()<UITableViewDelegate,UITableViewDataSource>{
UITableView *mytableView;
}
@end
@implementation testTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
mytableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 500)];
mytableView.rowHeight = 40.0;
mytableView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:mytableView];
mytableView.delegate = self;
mytableView.dataSource = self;
}
很有意思的事情是,在swift3.0里面曙咽,如果僅僅接受了這個TableView的Delegate蛔趴,就會報出錯誤的** Type 'SecViewController' conform to protocol 'UITableViewDataSource' **,雖然說這個讓swift變得非忱欤“安全”,但是也會讓初次使用swift的人感到迷茫(尼瑪P⑶椤)。在Objective-C中也會有警告提醒洒嗤。相對而言這讓人避免了一個錯誤箫荡,是真的!這是真的渔隶!
TableView Delegate簡單的‘三板斧’
所謂最簡單的三板斧羔挡,包括了:
- cell row number
- UITableViewCell
- didSelected
不多說洁奈,先上代碼:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 24;
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("選中了:\(IndexPath.row)\n")
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell.init(style: UITableViewCellStyle.default, reuseIdentifier: "cellIdentifier")
//說起來你也許不信,我在這里被坑了绞灼,不然你們可以試下cell.textLabel?.text = "test 數(shù)據(jù)"(說到底都是安全惹的禍)
cell.textLabel!.text = "test 數(shù)據(jù)"
//這里同樣具備UILabel 的特性可以進一步擴展
cell.textLabel!.textColor = UIColor.blue
cell.textLabel?.textAlignment = .center
/*
此處略去三百種寫法@酢!低矮!
*/
return cell
}
swift 3.0 code
這一段是Objective-C的印叁。
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 24;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellIdentifier"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellIdentifier"];
cell.backgroundColor = [UIColor blueColor];
cell.textLabel.text = @"test 數(shù)據(jù)";
cell.textLabel.textAlignment = NSTextAlignmentCenter;
}
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
printf("選中了:(index:%ld)\n",(long)indexPath.row);
}
objective-c
關(guān)于自定義的Cell
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:SwTableViewCell = tableView.dequeueReusableCell(withIdentifier: "cellIdentifer", for: indexPath) as! SwTableViewCell
cell.titleLab.text = "這里是可以寫字的"
cell.titleImage.backgroundColor = UIColor.blue//其實可以放圖片,不過我沒有!I谭稹:砀帧!
return cell
}
swift 3.0
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//這里其實很不規(guī)范良姆,因為我cell的類的第一個字符居然特么的小寫3λ洹!B曜贰K翱巍!H省(不過懶得改了:妗)
mytestTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellIdentity" forIndexPath:indexPath];
//我不太像公開關(guān)于cell的內(nèi)容,感興趣的可以去看一下陆馁,后面有彩蛋找颓!
if (cell == nil) {
cell = [[mytestTableViewCell alloc] init];
}
cell.titleImage.backgroundColor = [UIColor greenColor];
return cell;
}
Objective-C
來自遠方的cell發(fā)回來的Delegate
在多數(shù)情況下,我們需要在使用cell的同時叮贩,直接把cell里面的東西直接發(fā)到主頁面中(也就是剛剛說的TableView)击狮,這時候最好的就是通過代理的方式實現(xiàn),原因是因為省事R胬稀1肱睢!(作為程序猿捺萌,我們能有多懶就做多懶5刀!L掖俊)
先看下cell的代碼:
import UIKit
protocol SwTableViewCellDelegate {
func touchTheUIimage()
}
class SwTableViewCell: UITableViewCell {
@IBOutlet weak var titleLab: UILabel!
@IBOutlet weak var titleImage: UIImageView!
var delegate:SwTableViewCellDelegate?
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
//這里才是添加手勢的地方?崾摹!L埂G荷!M耘洹D锢着茸!
let touch = UITapGestureRecognizer.init(target: self, action: #selector(touchAcion));
titleImage.addGestureRecognizer(touch);
}
func touchAcion(){
//這里是添加手勢達到觸控的目的!琐旁!
delegate?.touchTheUIimage()
}
swift 3.0
這時候涮阔,還需要在主TableView中實現(xiàn)代理協(xié)議
//先添加協(xié)議
class SecViewController: UIViewController ,UITableViewDataSource,UITableViewDelegate ,SwTableViewCellDelegate{
//////
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:SwTableViewCell = tableView.dequeueReusableCell(withIdentifier: "cellIdentifer", for: indexPath) as! SwTableViewCell
cell.titleLab.text = "這里是可以寫字的"
cell.titleImage.backgroundColor = UIColor.blue//其實可以放圖片,不過我沒有!;遗埂>刺亍!
cell.delegate = self
return cell
}
//實現(xiàn)協(xié)議代理
func touchTheUIimage() {
print("說起來牺陶,也是簡單")
}
swift 3.0
下面看看Objective-C的
//這下面是在cell的.h文件實現(xiàn)的內(nèi)容
@protocol tableCellDelegate <NSObject>
@optional
- (void)touchImage;
@end
@interface mytestTableViewCell : UITableViewCell
@property (assign,nonatomic) id<tableCellDelegate> delegate;
///這個是在cell的.m文件里面實現(xiàn)的
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
UITapGestureRecognizer *touch = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touchImageAction)];
[_titleImage addGestureRecognizer:touch];
}
- (void)touchImageAction{
if ([_delegate respondsToSelector:@selector(touchImage)]) {
[_delegate touchImage];
}
}
Objective-C
然后需要在TableView的里面實現(xiàn)接收這個協(xié)議的代理方法(真是費勁伟阔,特么究竟是代理協(xié)議還是協(xié)議代理?掰伸?皱炉??狮鸭?:辖痢!F缃丁)
/// 這里是開始的interface
@interface testTableViewController ()<UITableViewDelegate,UITableViewDataSource,tableCellDelegate>
///這里才是添加協(xié)議的位置:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//這里其實很不規(guī)范灾部,因為我cell的類的第一個字符居然特么的小寫!9咄恕6乃琛!4吖颉4好帧(不過懶得改了!)
mytestTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellIdentity" forIndexPath:indexPath];
//我不太像公開關(guān)于cell的內(nèi)容叠荠,感興趣的可以去看一下,后面有彩蛋扫责!
if (cell == nil) {
cell = [[mytestTableViewCell alloc] init];
}
cell.delegate = self;
cell.titleImage.backgroundColor = [UIColor greenColor];
return cell;
}
-(void)touchImage{
NSLog(@"這里實現(xiàn)了協(xié)議");
}
Objective-C
寫到這里感覺好像TableView的基本簡單三板斧就差不多了榛鼎。
至于想要更多的用法,還是需要更深入的去閱讀apple的開發(fā)API鳖孤,雖然賊蛋疼者娱,寫的也不如一些優(yōu)秀開源網(wǎng)站那么好,但是還是需要耐心去閱讀苏揣。
出來混黄鳍,總是要還的!這不是無間道唬人的平匈,在開發(fā)上也是一樣的框沟!
當初被你拋棄的知識和基礎藏古,最終會在時間的長廊上遇上,然后再次被虐的體無完膚忍燥!這不是瓊瑤奶奶的小說拧晕,這是開發(fā)者的宿命!C仿ⅰ厂捞!如果愛,請深愛??