UI UITableView表视图

来源:互联网 发布:网络诈骗的手段有 编辑:程序博客网 时间:2024/05/16 14:40
<span style="font-size:18px;">- (void)viewDidLoad{    [super viewDidLoad];    // Do any additional setup after loading the view.        //UITableView继承自UIScrollView,所以可以滚动    //表视图的每⼀条数据都是显⽰在UITableViewCell对象中    //表视图可以分区显示数据,每个分区称为⼀个section,每⼀行称为 row,编号都是从0开始        UITableView *tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 480) style:UITableViewStylePlain];    //设置行高    tableView.rowHeight=80;    //设置分割线样式    tableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;    //设置分割线颜色    tableView.separatorColor=[UIColor redColor];    //设置数据源  提供数据源    tableView.dataSource=self;    //设置代理    tableView.delegate=self;        [self.view addSubview:tableView];    [tableView release];}#pragma mark------------#pragma mark 设置tableView分区数- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{    return 2;}#pragma mark 设置区头标题- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{    if (section==0) {        return @"A";    } else if(section==1){        return @"B";    }    return @"区头";}#pragma mark 设置区尾标题- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{    return @"区尾";}#pragma mark 设置右侧分区索引- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{    NSArray *titles=@[@"A",@"B",@"C",@"D"];    return titles;}#pragma  mark 设置每区的行数- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    NSLog(@"一共10行。。。。");    return 10;}#pragma mark 设置cell- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    /*     tableView重用机制:当cell移除屏幕时,会被放到tableView的重用队列中,当显示cell时,tableView会根据重用标识在重用队列中取出cell,显示到视图上。     如果tableView能够显示n个cell,那么在tableView创建时,会创建n个cell,当移动时(上一个cell移出一半,下一个cell显示一部分时)会创建第n+1个cell。     如果移动很快,还可能创建n+2,n+3。。。个cell,因为移动过快,tableView还来不及从重用队列取出重用cell,就需要显示,所以要创建     */    //先查看是否有重用的cell    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"AA"];    //如果没有,需要创建    static int count=0;//记录创建cell的个数    if (cell==nil) {        //注意内存管理        cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"AA"] autorelease];        //设置cell被选中时的样式  设置辅助视图    右面是箭头        cell.selectionStyle=UITableViewCellSelectionStyleDefault;        count++;        NSLog(@"创建第%d个cell",count);    }else{        NSLog(@"重用");    }    //设置文本    cell.textLabel.text=@"张三";    //设置图片    UIImage *img=[UIImage imageNamed:@"3.png"];    cell.imageView.image=img;    cell.detailTextLabel.text=@"xxx";    //如果有,不需要创建    return cell;}#pragma mark 设置某一行的高度- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{    if (indexPath.row%2==0) {        return 80;    } else {        return 50;    }}#pragma mark 自定义区头视图- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{    //此处设置的高度是不管用的必须在heightForHeaderInSection中设置区头高度    UIView *headerView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];    headerView.backgroundColor=[UIColor redColor];    return [headerView autorelease];}#pragma mark 设置区头高度- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{    return 50;}#pragma mark 选中某行- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    NSLog(@"选中某一行");    //页面切换    if (indexPath.section==0&&indexPath.row==1) {        SecondViewController *secondVC=[[SecondViewController alloc]init];        [self.navigationController pushViewController:secondVC animated:YES];        [secondVC release];    }}</span>

0 0
原创粉丝点击