iOS之tableView基本用法

來源:互聯網
上載者:User

標籤:

</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基本用法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.