標籤:style blog http color io os ar 使用 for
案例效果:
(1)先在storyboard中拖拽出一個tableView,然後以下用代碼。
——tableView繼承自scrollView,所以自然有滾動的特性
——最主要的還是資料轉模型,以及對cell的賦值
——而cell的賦值那一塊,為了最佳化效能,我們先從tableView的緩衝中尋找有無被緩衝的cell,如果有,直接取出,如果沒有再建立,這樣提高效能。
——這個緩衝池是tableView內建的,當滾動的時候,cell不在視線範圍內時,這個cell就被放到緩衝池裡了。
#import "ViewController.h"#import "WSHeros.h"@interface ViewController ()<UITableViewDataSource>@property (weak, nonatomic) IBOutlet UITableView *tableView;@property (strong,nonatomic) NSArray *herosArray;@end@implementation ViewController- (void)viewDidLoad { //定義資料來源是誰 self.tableView.dataSource=self; //每行高度 self.tableView.rowHeight=60; //行間分割線顏色和樣式 self.tableView.separatorColor=[UIColor colorWithRed:0 green:0 blue:1 alpha:1]; self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone; [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib.}//隱藏狀態列-(BOOL)prefersStatusBarHidden{ return YES;}//設定多少行-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return self.herosArray.count;//數組有多少就多少行}//設定每行的cell內容-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *[email protected]"hero"; //1、先判斷tableView中有無我們需要的緩衝的cell,用ID類識別 UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:ID]; //2、如果沒有,就直接建立,記得給ID識別號 if (cell==nil) { cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID]; } //不管是直接取,還是直接建立,截止此處,有一個cell了。 //先利用indexPath.row取得行號對應的模型 WSHeros *hero=self.herosArray[indexPath.row]; //然後給cell賦值 cell.textLabel.text=hero.name; cell.imageView.image=[UIImage imageNamed:hero.icon]; cell.detailTextLabel.text=hero.intro; cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator; //cell還有背景以及選中背景等選項 cell.backgroundColor=[UIColor grayColor]; UIView *bgView=[[UIView alloc]init]; bgView.backgroundColor=[UIColor redColor]; cell.selectedBackgroundView=bgView; return cell;}//字典轉模型-(NSArray *)herosArray{ if (_herosArray==nil) { NSString *path=[[NSBundle mainBundle]pathForResource:@"heros.plist" ofType:nil]; NSArray *arr=[NSArray arrayWithContentsOfFile:path]; NSMutableArray * herosMuArr=[[NSMutableArray alloc]init]; for (NSDictionary * dict in arr) { WSHeros *heros=[[WSHeros alloc]initWithDict:dict]; [herosMuArr addObject:heros]; } _herosArray=herosMuArr; } return _herosArray;}@end
(2)增加點擊彈出alert的效果,並且可以修改名字
——因為用到監聽tableView點擊,所以需要引入協議
——因為用到監聽使用者點擊了Alert裡的哪個按鈕,所以需要協議
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,UIAlertViewDelegate>
並且需要設定代理(Alert的代理,在建立的時候直接指定為self即可)
- (void)viewDidLoad { …… self.tableView.delegate=self; ……}
——監聽tableView被點擊時,需要彈出一個帶有textField的框,可以用alert.alertViewStyle屬性設定,並且把這個模型裡面的名字賦值給文字框顯示出來。
——此外,還需要把是第幾個模型,這個數字記錄下來,在下一個方法監聽點擊alert哪個按鈕的哪個裡面需要用到,正好記錄到alert的tag中。
——監聽是否點擊的時“確定”按鈕,如果是,則先擷取文字框文字,然後利用alert.tag找到對應的資料模型,用這個獲得的文字替換原來的模型資料,最後重新整理一下tableView,只有修改資料模型,才能徹底改變tableView的顯示結果,否則只修改tableView,等重新載入的時候還是會顯示原來的資料,因為資料模型沒有修改。
——思想是:用資料模型控制視圖,即修改了資料模型,視圖的呈現自然跟著修改。
——這裡用到的知識點相對較多,涉及到tableView的reloadData方法。
——涉及到alertView得樣式設定方法。
——還涉及到使用tableView和alertView的代理來實現監聽。
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ WSHeros *hero=self.herosArray[indexPath.row]; UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"提示" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"好的", nil]; alert.alertViewStyle=UIAlertViewStylePlainTextInput; [alert textFieldAtIndex:0].text=hero.name; alert.tag=indexPath.row; [alert show];}-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ if (buttonIndex==0) return; WSHeros *hero=self.herosArray[alertView.tag]; hero.name=[alertView textFieldAtIndex:0].text; //這裡需要傳遞一個indexPath數組,因為可能重新整理不止一行,所以需要知道是幾組幾行,然後把很多個組成數組傳遞進去 NSIndexPath *path=[NSIndexPath indexPathForRow:alertView.tag inSection:0]; [self.tableView reloadRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationBottom]; //以下是重新整理全部資料// [self.tableView reloadData];}
最終效果:
【iOS開發-59】LOL案例:單組tabView、alertView樣式、實現監聽,以及用reloadData資料重新整理