對於UITableView的基礎使用我這邊就不做重復介紹了
我重點就來介紹下如何實現大部分新聞的界面。也就是第一條記錄顯示大圖片下面加一段文字說明
然后剩下來的內容全部顯示為文字圖片的格式

其實要做到這樣的效果是非常容易。
我們首先先了解一下UITableView的幾個方法
//分組數
//如果我們的數據有分組的話 那就需要在.M文件中加入該方法,並返回分組數有多少組數據 就返回多少
//我一般是在做個人信息頁面或者信息提交等頁面的時候會用到分組的樣式
//需要注意的是此方法與 返回分組 每組數據個數的方法 以及 分組組名 方法 一起使用
-(NSInteger) numberOfSectionsInTableView
//分組組名
//此方法是用來設置 分組組名的 我們就可以根據section的值來判斷是第幾組,然后返回不同的標題,如果沒有 標題的話 可以返回 @""
-(NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:
//分組每組的個數
//此方法用來返回分組的每組個數。如果分組是固定的話 那么可以返回固定的值。
//而對於一些分組是特殊的 則可以根據section的值來判斷是第幾組 接着再返回對應的組數
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:
//行選擇事件
//顧名思義 就是行選擇事件,此事件會在你點擊了對應的行之后觸發,一般對於新聞頁面來說就是跳轉到新聞的詳細頁面
//而對於一些個人信息修改的操作的話則可以進入到相對應的個人信息修改界面。
//大家可以自行根據IndexPath 的Row和Section來進行判斷點擊的是哪個然后進行一些特殊的處理
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:
//行高度事件
//此方法是用來設置UITableView每一行的行高的事件。如果不重寫此方法的話他會返回默認的值
//如果要實現上面的效果的話 我們就必須要重寫該方法。
//我們要做到的效果就是第一條數據顯示大圖片,后面的顯示普通記錄
//所以就要修改該方法 判斷 indexpath.row ==1 如果等於則返回200 否則返回50 (具體高度根據實際情況返回)
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//我稱該方法為行繪制事件
//此方法就是本文的重中之重了,所有的效果均由此方法來實現
//UITableView的行繪制全部在這里實現。(這邊當時出了小BUG還是比較糾結的。以后會講到)
//我們如果能控制好該方法 那么基本上能利用UITableView實現大部分的界面效果了
//我們可以這么理解 其實 UITableView 只是一個容器, 就類似於一個list 類
//而我們的UITableViewCell 才是里面具體顯示的效果
//所以如果要實現各種各樣的效果 我們只需要“定制”特定的UITableViewCell然后根據相關的數據 選擇使用哪一種UITableViewCell的樣式就可以了
-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:
本篇就講這么多吧。主要內容就是 將UITableView的幾個方法 單獨拿出來講解了 一下(紅色字體為重要內容,相信只要理解了該部分就能很好的使用UITableView了)
下一篇會講如何自定義 UITableViewCell
另外附上一段
-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:
的代碼 供大家參考(只是我個人比較笨的使用方法)
1 -(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 2 { 3 NSString *haveImage=[[NSString alloc] initWithString:[[[_list objectAtIndex:[indexPath section]] objectAtIndex:[indexPath row]] valueForKey:@"images"]]; 4 if([haveImage isEqual:@"False"]) 5 { 6 //static NSString *SignleNoImageIdentifier=@"SignleIdentifier"; 7 static NSString *SignleNoImageIdentifier=@"SignleNoImageIdentifier"; 8 SignleNoImageTableViewCell *cell= (SignleNoImageTableViewCell *)[tableView dequeueReusableCellWithIdentifier:SignleNoImageIdentifier]; 9 if(cell==nil){ 10 NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SignleNoImageTableViewCell" owner:self options:nil]; 11 cell = [nib objectAtIndex:0]; 12 } 13 cell.mylabel.text=[[[_list objectAtIndex:[indexPath section]] objectAtIndex:[indexPath row]] valueForKey:@"title"]; 14 return cell; 15 } 16 else 17 { 18 if([indexPath row]==0) 19 { 20 // static NSString *TopbigImageIdentifier=@"SignleIdentifier"; 21 static NSString *TopbigImageIdentifier=@"TopbigImageIdentifier"; 22 TopBigImageCell *cell= (TopBigImageCell *)[tableView dequeueReusableCellWithIdentifier:TopbigImageIdentifier]; 23 if(cell==nil){ 24 NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TopBigImageCell" owner:self options:nil]; 25 cell = [nib objectAtIndex:0]; 26 } 27 cell.btlabel.text=[[[_list objectAtIndex:[indexPath section]] objectAtIndex:[indexPath row]] valueForKey:@"title"]; 28 NSData *newimage=[Base64AndImageHelp mydataWithBase64EncodedString:[[[_list objectAtIndex:[indexPath section]] objectAtIndex:[indexPath row]] valueForKey:@"images"]]; 29 UIImage *newjiaban=[[UIImage alloc] initWithData:newimage]; 30 cell.myimageView.image=newjiaban; 31 return cell; 32 33 } 34 else 35 { 36 static NSString *SignleIdentifier=@"SignleIdentifier"; 37 SingleTableViewCell *cell= (SingleTableViewCell *)[tableView dequeueReusableCellWithIdentifier:SignleIdentifier]; 38 if(cell==nil){ 39 NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SingleTableViewCell" owner:self options:nil]; 40 cell = [nib objectAtIndex:0]; 41 } 42 cell.mylabel.text=[[[_list objectAtIndex:[indexPath section]] objectAtIndex:[indexPath row]] valueForKey:@"title"]; 43 NSData *newimage=[Base64AndImageHelp mydataWithBase64EncodedString:[[[_list objectAtIndex:[indexPath section]] objectAtIndex:[indexPath row]] valueForKey:@"images"]]; 44 UIImage *newjiaban=[[UIImage alloc] initWithData:newimage]; 45 cell.myimageView.image=newjiaban; 46 return cell; 47 48 } 49 50 51 } 52 }
