剛轉(zhuǎn)行iOS的搬磚工人,在此記錄下這條路上的點點滴滴,共勉
效果圖:
QQ-好友動態(tài)
分析:
其實這個頁面就是一個UITableView,但是在tableView的最上方添加了一插入了一張圖片,所以看起來很有逼格。
So問題來了,這個效果怎么實現(xiàn)呢?
實現(xiàn):
首先,新建一個工程,將.h文件的繼承從UIViewController改成UITableViewController:
@interface ViewController : UITableViewController
接著,刪除storyboard中原來的ViewController,添加一個TableViewController,在屬性項中勾選上Is Initial View Controller(即啟動程序后進入這個表視圖):
重新添加一個UITableViewController
然后,這里節(jié)省點時間,就直接在故事版中添加數(shù)據(jù),選中的表視圖的cell,添加標識符(Identifier)為,我這里命名的cell;再加入一個label,內(nèi)容隨便寫:
在故事版中添加cell的內(nèi)容
回到.m文件中,寫入UITableView最基本的2個方法:
//要顯示的行數(shù)
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 10;
}
// 返回指定的row的cell(即在故事版中編輯的cell的內(nèi)容)
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"cell"];
return cell;
}
最關(guān)鍵的來了(前面的都是廢話,這里才是主題):
新建一個方法myCustomImage(英語不好,輕噴),用來實現(xiàn)QQ好友動態(tài)最上方的背景圖片的效果:
//自定義的方法,表視圖最上方的添加一張圖片的效果
- (void)myCustomImage{
//設(shè)置要顯示的圖片
UIImageView *customImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"bg"]];
//圖片位置大小
customImageView.frame = CGRectMake(0, 0, _table.frame.size.width,0);
//圖片填充方式(很關(guān)鍵的地方。填充方式很多種,有興趣的可以去了解下contentMode這個屬性)
customImageView.contentMode = UIViewContentModeBottom;
//自動調(diào)整滾動視圖的插圖
self.automaticallyAdjustsScrollViewInsets = YES;
//顯示在tableView上
_table.tableHeaderView = customImageView;
//出現(xiàn)位置(即我們看到的多少。第一個參數(shù)越大,即圖片出現(xiàn)的位置越往下,我們看到的圖片內(nèi)容越多)
_table.contentInset = UIEdgeInsetsMake(200, 0, 0, 0);
}
運行效果:
啟動后界面
下拉效果:
進行下拉操作
完整代碼:
#import "ViewController.h"
@interface ViewController ()
@property (strong, nonatomic) IBOutlet UITableView *table;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.tableView.delegate = self;
self.tableView.dataSource = self;
//調(diào)用myCustomImage方法
[self myCustomImage];
//刷新tableView
[self.tableView reloadData];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 10;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"cell"];
return cell;
}
- (void)myCustomImage{
//設(shè)置要顯示的圖片
UIImageView *customImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"bg"]];
//圖片位置大小
customImageView.frame = CGRectMake(0, 0, _table.frame.size.width,0);
//圖片填充方式(很關(guān)鍵的地方。填充方式很多種,有興趣的可以去了解下contentMode這個屬性)
customImageView.contentMode = UIViewContentModeBottom;
//自動調(diào)整滾動視圖的插圖
self.automaticallyAdjustsScrollViewInsets = YES;
//顯示在tableView上
_table.tableHeaderView = customImageView;
//出現(xiàn)位置(即我們看到的多少。第一個參數(shù)越大,即圖片出現(xiàn)的位置越往下,我們看到的圖片內(nèi)容越多)
_table.contentInset = UIEdgeInsetsMake(200, 0, 0, 0);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
PS:感覺寫的有點馬虎了,也不知道有沒有人看。但是堅持寫下去吧,要對自己負責。