無標題文章

?UITableView?*DataTable;

? ??NSMutableArray?*dataArray1;?//定義數據數組1

? ??NSMutableArray?*dataArray2;//定義數據數組2

? ??NSMutableArray?*titleArray;//定義標題數組

}

- (void)viewDidLoad

{

? ? [superviewDidLoad];

//初始化tableview

? ??DataTable?= [[UITableViewalloc]?initWithFrame:CGRectMake(0,?0,?320,?420)];//指定位置大小

? ? [DataTablesetDelegate:self];//指定委托

? ? [DataTablesetDataSource:self];//指定數據委托

? ? [self.viewaddSubview:DataTable];//加載tableview

? ??dataArray1?= [[NSMutableArrayalloc]?initWithObjects:@"中國",?@"美國",?@"英國",?nil];//初始化數據數組1

? ??dataArray2?= [[NSMutableArrayalloc]?initWithObjects:@"黃種人",?@"黑種人",?@"白種人",?nil];//初始化數據數組2

? ??titleArray?= [[NSMutableArrayalloc]?initWithObjects:@"國家",?@"種族",?nil];//初始化標題數組

}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

? ??// Return YES for supported orientations

? ??return?(interfaceOrientation ==?UIInterfaceOrientationPortrait);

}

//每個section顯示的標題

- (NSString?*)tableView:(UITableView?*)tableView titleForHeaderInSection:(NSInteger)section{

? ??switch?(section) {

? ? ? ??case?0:

? ? ? ? ? ??return?[titleArray?objectAtIndex:section];//提取標題數組的元素用來顯示標題

? ? ??? case 1:

? ? ? ? ? ??return?[titleArray?objectAtIndex:section];//提取標題數組的元素用來顯示標題

? ? ? ??default:

? ? ? ? ? ??return?@"Unknown";

? ? }

}

//指定有多少個分區(Section),默認為1

- (NSInteger)numberOfSectionsInTableView:(UITableView?*)tableView {

? ??return?[titleArray count];//返回標題數組中元素的個數來確定分區的個數

}

//指定每個分區中有多少行,默認為1

- (NSInteger)tableView:(UITableView?*)tableView numberOfRowsInSection:(NSInteger)section{

? ??switch?(section) {

? ? ? ??case?0:

?? ? ? ? ??return? [dataArray1?count];//每個分區通常對應不同的數組,返回其元素個數來確定分區的行數

? ? ? ? ? ??break;

? ? ? ??case?1:

? ? ? ? ? ??return? [dataArray2?count];

? ? ? ? ? ??break;

? ? ? ??default:

? ? ? ? ? ??return?0;

? ? ? ? ? ??break;

? ? }

}

//繪制Cell

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

? ??static?NSString?*CellIdentifier =?@"Cell";

?//初始化cell并指定其類型,也可自定義cell

UITableViewCell?*cell = (UITableViewCell*)[tableView??dequeueReusableCellWithIdentifier:CellIdentifier];

  if(cell ==?nil)?

  {

  cell = [[[UITableViewCellalloc]?

  initWithFrame:CGRectZero?

  reuseIdentifier:CellIdentifier]?autorelease];

}

?  switch?(indexPath.section) {

  case 0://對應各自的分區

    [[cell?textLabel] ?setText:[dataArray1 objectAtIndex:indexPath.row]];//給cell添加數據

    break;

  case 1:

    [[cell?textLabel] ?setText:[dataArray2 objectAtIndex:indexPath.row]];

    break;

  default:

    [[cell?textLabel] ?setText:@"Unknown"];

}

  return?cell;//返回cell

}

tableview還有很多高難度的屬性和接口,在以后我會慢慢補齊。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • UITableView*DataTable; NSMutableArray*dataArray1; NSMut...
    畫個完美句號閱讀 284評論 0 0
  • //// AddressBookViewController.h// QRCodeScanApp//// Crea...
    奇怪的知識增加了閱讀 318評論 0 0
  • 【 男友力一:愛她就幫她清空購物車】 明明樓下超市就可以買到的東西,為什么女生偏要網購呢? 因為女人天生就是享受收...
    不愛說話的小喵閱讀 533評論 0 0
  • 一月的風很涼 吹皺了二月的云 三月的暖陽下 藏著四月的倉皇 五月的你的笑顏 在六月里綻放 七月我哼著歌 路過八月的...
    七夏呀閱讀 259評論 1 2
  • 她是佛祖坐下千年得道的白狐,化了一張絕世美人的面貌,卻心狠手辣。 他是捉妖師,對妖類一視同仁。 她愛上了他, 因為...
    落葉盡殤閱讀 495評論 0 0