標籤:
</pre><p>@implementation ViewController- (void)viewDidLoad {<span style="font-size:12px"> [super viewDidLoad]; UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height -64 ) style:UITableViewStylePlain];//注意問題:tableStyle還有一個grouped類型,用這個會在tableView上方出現空白</span></p><p><span style="font-size:12px"> tableView.delegate = self; tableView.dataSource = self; //設定表頭// UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 80)];// view.backgroundColor = [UIColor redColor]; //將一張圖片作為表頭 UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 150)]; imageV.image = [UIImage imageNamed:@"tu.jpg"]; tableView.tableHeaderView = imageV; [self.view addSubview:tableView]; </span> //分區 </p><p class="p1" style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><span class="s1"> [</span><span class="s2">tableview </span>setSeparatorColor<span class="s1">:[</span>UIColor <span class="s1"></span>blueColor<span class="s1">]]; //設定分割線為藍色</span></p><p class="p1" style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><span class="s1"></span></p><p class="p1" style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px">隱藏UITableViewCell的分隔線</p><p class="p1" style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px">[self.myTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone]; </p><p class="p2" style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"></p><p class="p1" style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"> UITableViewCellSeparatorStyle有如下幾種 </p><p class="p1" style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><span class="s1">typedef</span> <span class="s2">NS_ENUM</span>(NSInteger, UITableViewCellSeparatorStyle) {</p><p class="p1" style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"> UITableViewCellSeparatorStyleNone,</p><p class="p1" style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"> UITableViewCellSeparatorStyleSingleLine,</p><p class="p2" style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><span class="s3"> UITableViewCellSeparatorStyleSingleLineEtched </span>// This separator style is only supported for grouped style table views currently</p><p class="p1" style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px">};</p>}<p class="p1" style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"></p>-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *indentifier = @"cell1"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:indentifier]; } cell.textLabel.text = @"分區和表頭"; return cell;}-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ //section代表分區 第一分區兩行,其他分區3行 if (section == 0) { return 2; }else if(section== 1){ return 3; }else{ return 1; }// return 5;//每個分區都是5行}//分區個數-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 3;}//分區高度-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ return 60;}//cell高度-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 60;}//分區的標題-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ if (section == 0) { return @"A"; } else if(section == 1){ return @"B"; }else{ return @"C"; } }//自訂分區的樣式-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 80)]; view.backgroundColor = [UIColor greenColor]; return view;}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}<p>@end</p><p></p><pre name="code" class="objc">一個section重新整理NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:2];[tableview reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];//一個cell重新整理NSIndexPath *indexPath=[NSIndexPath indexPathForRow:3 inSection:0];[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone]; http://www.cnblogs.com/wendingding/p/3801454.html發現讓tableview 滾動到頂部 這句話是最簡單方便的了[myTB setContentOffset:CGPointMake(0,0) animated:NO];
//
iOS之tableView基本用法