uitableview學習---1,uitableview---1
#import "ViewController.h"@interface ViewController ()@property (strong,nonatomic) NSArray *bj;@property (strong, nonatomic)NSArray *gd;@end@implementation ViewController- (void)viewDidLoad{ [super viewDidLoad]; self.bj = @[@"東城", @"西城", @"宣武", @"崇文"]; self.gd = @[@"廣東", @"佛山", @"深圳"];}#pragma mark -TableView的資料來源方法#pragma mark 分組數- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 2;}#pragma mark 每個分組中顯示的表格個數,指定每個分組中的行數- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ if (section == 0) { return self.bj.count; }else{ return self.gd.count; }}#pragma mark 每個表格中顯示的資料內容,這裡面的內容是由資料來源負責的。//@interface NSIndexPath (UITableView)////+ (NSIndexPath *)indexPathForRow:(NSInteger)row inSection:(NSInteger)section;////@property(nonatomic,readonly) NSInteger section;//@property(nonatomic,readonly) NSInteger row;////@end//可以通過當前的indexPath判斷出當前的行和組- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil]; NSString *cityName = nil; //組 if(indexPath.section == 0) //0組 { cityName = _bj[indexPath.row]; }else{ cityName = _gd[indexPath.row]; } [cell.textLabel setText:cityName]; return cell;}#pragma mark 分組的標題- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ return section == 0 ? @"北京" : @"廣東";}#pragma mark 分組底部的子標題,通常用於描述- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{ return section == 0 ? @"北京有烤鴨" : @"廣東有東莞";}@end
小弟剛接觸xcode的編程,想學一下表格(UITableView)方面的基本操作,給出詳細指導,非常感謝
這裡完全打不下那麼多字啊。給LZ一個簡要的學習過程吧。
TableView主要遵從兩個協議,一個叫TableViewDataSource,另一個叫TableViewDelegate。看名字就知道一個叫資料來源,一個叫委託,無論LZ用何種方式建立表視圖(IB或者純程式碼),都需要設定表視圖的委託和資料來源,而被設定為表視圖委託和資料來源的類就需要實現上文所說的兩個協議中的方法,否則是不能響應表視圖的委託請求和資料請求的。所以學習方法也就很明白了,開啟IDE,查看前文所述兩個協議的具體內容(都有英文注釋的哦),在你自己的類中實現他們即可。熟悉了表視圖之後就要關注一下它的父類。
UITableView繼承自UIScrollView(滾動視圖),既然已經熟悉了表視圖,熟悉一下它的父視圖是沒壞處的,畢竟一些表視圖的效果(比如滾動重新整理什麼的)是需要用到滾動視圖的方法和資料成員的。
最後,表視圖是IOS系統UI中重要的組成不分,是非常重要的一個控制項,預祝LZ學習順利
iOS7下,UINavigation中加入Grouped類型的UITableViewCell會有一段空白,怎調整?
cell是不分類型的,只有UITableView會分Plain和Group類型, 還有 UINavigationBar上邊一般也不加Cell,所以搞不清楚樓主想要做什麼。。