最近做項目又開始用到了uitableview,溫習之余,在這里把uitableview的用法分享一下,有不對的地方歡迎大家提出來。
廢話不多說,先創建一個工程,由于Xcode6,去除了創建工程時的空項目的選項,我們繼續選擇single view application 在這里我們用不到main storyboard 先刪掉,創建一個類,繼承自
UINavigationController ,這里文件名字叫做HealthViewcont
然后在appdelegate里的
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
return YES;
}
添加如下代碼:
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds]; [self.window makeKeyAndVisible]; self.window.rootViewController = [[HealthViewcont alloc]init];
準備工作就完成了
-:UITableView的初始化
1.在.h文件里實現 UITableViewDataSource,UITableViewDelegate兩個代理協議,如果你這里繼承的時UITableView 可以不用寫
然后定義兩個對象
@PRoperty(nonatomic)UITableView* tableview; @property(nonatomic)NSMutableArray* dataArryList;
在.m文件里實現
@synthesize tableview;@synthesize dataArryList;
2.在viewdidload里添加如下代碼
- (void)viewDidLoad { [super viewDidLoad]; //初始化一個tableview tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-64)]; [self.view addSubview:tableview]; [self.navigationBar setBackgroundColor:[UIColor redColor]]; //實現代理 tableview.delegate = self; tableview.dataSource = self; //初始化數據 dataArryList = [[NSMutableArray alloc]initWithArray:[NSArray arrayWithObjects:@"334", @"445",@"667",@"779",@"123",nil]]; // Do any additional setup after loading the view, typically from a nib. }
到這里初始化就完成了
二:UITableView數據源的初始化
UITableView有三個必須要實現的代理方法
#pragma mark - Table View//設置section的個數- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1;}//設置每個section 的行數- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [dataArryList count];}//對每個TableViewCell進行操作 UITableView中顯示的每一個單元都是一個UITableViewCell對象,其初始化函數initWithStyle:reuseIdentifier:在tableView快速滑動的滑動的過程中,頻繁的alloc對象是比較費時的///,于是引入了cell的重用機制- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString* cellIdentifier = @"cell"; UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if (cell== nil) { cell= [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] ; } cell.textLabel.text=[dataArryList objectAtIndex:indexPath.row]; return cell;}
三:插入和刪除
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { [self.dataArryList removeObjectAtIndex:indexPath.row]; [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; } else if (editingStyle == UITableViewCellEditingStyleInsert) { [self.dataArryList insertObject:@"456" atIndex:indexPath.row]; [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight]; // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view. }}
四:其他的一些常用操作
//設置UITableView行縮進-(NSInteger)tableView:(UITableView*)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{ NSUInteger row = [indexPath row]; return row;}//設置cell行間隔的高度-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 60;}//設置選中Cell的響應事件-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{}//設置選中的行所執行的動作-(NSIndexPath*)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{ NSUInteger row = [indexPath row]; return indexPath;}//設置劃動cell是否出現del按鈕,可供刪除數據里進行處理-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {}//設置刪除時編輯狀態-(void)tableView:(UITableView*)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath{}
最后看一下運行的效果
新聞熱點
疑難解答