標籤:ios ios開發 objective-c uitableviewcell 下拉控制項
由於表應用有兩個UI設計模式: 分頁模式、下拉重新整理模式。
其中下拉重新整理被廣泛應用(新浪微博,QQ)
這裡吐槽一下QQ的墨跡,其實PC案頭的應用還好,及時的扁平化。但是IOS這麼大的市場,但是現在都IOS8了,為什麼還在用IOS6的下拉重新整理的“膠皮糖”UI樣式。
IOS6以後增加了一個UIRefreshControl的下拉重新整理類,目前這個類只能應用於表視圖介面。
在Xcode6中,還沒有在故事版中增加下拉拖拽,類似label之類的控制項。所以布局什麼的不用太多考慮。
代碼部分可重用,目前就當作一個庫來用吧。
H檔案:
#import <UIKit/UIKit.h>@interface TableViewController : UITableViewController@property (nonatomic, strong) NSMutableArray * Logs;@end
M檔案:
#import "ViewController.h"@interface TableViewController ()@end@implementation TableViewController- (void)viewDidLoad { [super viewDidLoad]; // 初始設定變數和時間 self.Logs = [[NSMutableArray alloc] init]; NSDate *date = [[NSDate alloc] init]; [self.Logs addObject:date]; // 初始化UIRefreshControl // rc為該控制項的一個指標,只能用於表視圖介面 // 關於布局問題可以不用考慮,關於UITableViewController會將其自動放置於表視圖中 UIRefreshControl *rc = [[UIRefreshControl alloc] init]; rc.attributedTitle = [[NSAttributedString alloc]initWithString:@"下拉重新整理"]; // 一定要注意selector裡面的拼字檢查 [rc addTarget:self action:@selector(refreshTableView) forControlEvents:UIControlEventValueChanged]; self.refreshControl = rc;}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}- (void) refreshTableView{ if (self.refreshControl.refreshing) {// 判斷是否處於重新整理狀態 self.refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString: @"載入中..."]; // 添加新的模擬器 NSDate *date = [[NSDate alloc] init]; // 類比請求完成以後,回調方法callBackMethod [self performSelector:@selector(callBackMethod:) withObject:date afterDelay: 3]; } }- (void) callBackMethod:(id) obj{ [self.refreshControl endRefreshing]; // 停止下拉重新整理控制項,回到初始狀態 self.refreshControl.attributedTitle =[[NSAttributedString alloc]initWithString:@"下拉重新整理"]; // 文本變回來 [self.Logs addObject:(NSDate *)obj]; [self.tableView reloadData];}#pragma mark --- UITableViewDataSource代碼- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{ return 1;}- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return [self.Logs count];}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ // 這可以說是Cell部分最重要的程式碼片段了 // 定義Cell,然後判斷cellIdentifier是否重用 static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier: CellIdentifier]; } // 然後擷取時間 NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init]; [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; // 然後更新cell cell.textLabel.text = [dateFormat stringFromDate:[self.Logs objectAtIndex:[indexPath row]]]; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; return cell;}@end
注意@selector()裡面是不會進行拼字檢查的,所以要保證拼字正確!
UI下拉重新整理(IOS開發)