无标题文章

 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
  • 她是佛祖坐下千年得道的白狐,化了一张绝世美人的面貌,却心狠手辣。 他是捉妖师,对妖类一视同仁。 她爱上了他, 因为...
    落叶尽殇阅读 489评论 0 0