iOS之tableView基本用法

</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];

//


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值