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

//

时间: 2024-08-03 19:08:07

iOS之tableView基本用法的相关文章

IOS中TableView的用法

IOS中TableView的用法 一.UITableView 1.数据展示的条件 1> UITableView的所有数据都是由数据源(dataSource)提供的,所以要想在UITableView展示数据,必须设置UITableView的dataSource数据源对象 2> 要想当UITableView的dataSource对象,必须遵守UITableViewDataSource协议,实现相应的数据源方法 3> 当UITableView想要展示数据的时候,就会给数据源发送消息(调用数据源

IOS学习中的TableView的用法

TableView的用法 TableView是继承ScrollView,在ScrollView还未出现之前,所有的滑动视图都是用TableView来定制的,TableView是必须要实现它自身的两个代理方法的,下面是TableView的一些基本属性和方法的应用 要注意的是(对于所有的ScrollView及其子类来说): 第一次添加到控制器视图上的子视图如果是ScrollView的对象(包含ScrollView子类的对象),则视图的内容偏移量会根据是否有导航栏和标签栏进行扩充 表视图创建时的基本样

iOS多线程技术—NSOperation用法

iOS多线程技术—NSOperation用法 一.NSOperation简介 1.简单说明 NSOperation的作?:配合使用NSOperation和NSOperationQueue也能实现多线程编程 NSOperation和NSOperationQueue实现多线程的具体步骤: (1)先将需要执行的操作封装到一个NSOperation对象中 (2)然后将NSOperation对象添加到NSOperationQueue中 (3)系统会?动将NSOperationQueue中的NSOperat

iOS 在TableView的Cell之间设置空白间隔空间

1.设置section的数目,即是你有多少个cell - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 3; // in your case, there are 3 cells } 2.对于每个section返回一个cell - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)secti

IOS中NSSData常见用法

一.NSdata的概念 1.使用文件时需要频繁地将数据读入一个临时存储区,它通常称为缓冲区 2.NSdata类提供了一种简单的方式,它用来设置缓冲区,将文件的内容读入缓冲区,或者将缓冲区内容写到一个文件. 3.对于32位应用程序,NSdata缓存最多2GB 4.我们有两种定义 NSData(不可变缓冲区),NSMutableData(可变缓冲区) NSData *fileData; NSFileManager *fileManager = [[NSFileManager alloc]init];

IOS中NSNumber常见用法

一.NSnumber常见用法 NSNumber + (NSNumber *)numberWithInt:(int)value; + (NSNumber *)numberWithDouble:(double)value; - (int)intValue; - (double)doubleValue; -(float)floatValue; 二.使用 NSNumber * intNumber=[NSNumber numberWithInt:100]: NSNumber *floatNumber=[N

【COCOS2DX-游戏开发之三四】cocos2dx 3.0 TableView特殊用法:滚动时不能选择等等

cocos2dx 3.0版本TableView拍生自ScrollView,常用来做滚动列表,有几种特殊用法,不知道大家用到过没 要求:1.滚动时不能选中TableCell,非滚动状态才能选中 很简单,在TableView的delegate函数中,通过isTouchMoved()函数来判断 void WeaponSelectLayer::tableCellUnhighlight(cocos2d::extension::TableView* table, cocos2d::extension::Ta

IOS中NSUserDefaults的用法(轻量级本地数据存储)

iOS数据保存 IOS中NSUserDefaults的用法(轻量级本地数据存储) IOS中NSUserDefaults的用法(轻量级本地数据存储),布布扣,bubuko.com

[iOS] Create TableView &amp; customize UITableViewCell

1. First artical, notice the last thing - Connecting the DataSource and Delegate: http://www.appcoda.com/ios-programming-tutorial-create-a-simple-table-view-app/ 2. Second Artical: https://medium.com/@musawiralishah/creating-custom-uitableviewcell-us