UITableView

来源:互联网 发布:vfp中关闭数据库的命令 编辑:程序博客网 时间:2024/05/16 19:28

tabview列表样式:

UITableViewStylePlain--不分组

UITableViewStyleGrouped--分组


numberOfSectionlInTableView--Section的数量

每个section的页眉和页尾--数据源的方法:tableView:titleForHeaderInSection, tableView:titleFooterInSection

返回字符串,默认是nil

页眉和页尾的视图:

tableView:viewForHeaderInSection,

tableView:viewFooterInSection


页眉和页尾的高度是默认由自表自身决定的,该表有两个属性:sectionHeaderHeght,sectionFooterHeight;可以通过委托方法来覆盖:tableView:heightForHeaderInSection,tableVive:heightForFooterInSection;


一般在UITableViewController的子类上进行UItableViewDelegate上的操作和UITableViewDataSource上的数据操作;


必须要实现的方法:

1.每个section有固定的行数的话就用:- (NSInteger)tableView:(UITableView *)tableView numberOfRpwsInSection:(NSInteger)section;

2.必须要实现的,返回某一行的cell数据-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;


NSIndexPath可以确定表单元的位置(section和row)


表视图的section返回0时表示没有数据,会阻止表视图的其他请求;


cell重用会调用次方法:

  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

{

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@”MyIdentifier”];

if(cell == nil)

{

cell = [[UITabelView alloc]initWithStyle:... reuseIdentifier:@””
}

}

  • (void)viewWillAppear:(BOOL)animated

{

[super viewWillAppear;animated];

[self.tableView reloadData];

}



视频笔记:

无格式表视图和分段表视图(普通,分组,索引)


每一行叫做单元格,可以放置控件.


封装成属性.



选中单元格后触发的方法:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath


//

//  RootViewController.m

//  20-02

//

//  Created by IT小怪才 on 12-11-2.

//  Copyright (c) 2012年 IT小怪才. All rights reserved.

//


#import "RootViewController.h"

#import "item.h"


@interfaceRootViewController ()


@end


@implementation RootViewController


@synthesize tableView;

@synthesize items;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [superinitWithNibName:nibNameOrNilbundle:nibBundleOrNil];

    if (self) {

       // Custom initialization

    }

    returnself;

}


- (void)viewDidLoad

{

    [superviewDidLoad];

// Do any additional setup after loading the view.


    items = [[NSMutableArrayalloc]initWithCapacity:20];


    tableView = [[UITableViewalloc]initWithFrame:CGRectMake(0,0,320,460)style:UITableViewStyleGrouped];

    

    tableView.delegate =self;

    tableView.dataSource = self;


    [self.viewaddSubview:tableView];


    item *msg;


    msg = [[itemalloc]init];

    msg.text =@"小葱拌豆腐";

    msg.checked =NO;

    [itemsaddObject:msg];


    msg = [[itemalloc]init];

    msg.text =@"木桶饭";

    msg.checked =YES;

    [itemsaddObject:msg];


    msg = [[itemalloc]init];

    msg.text =@"姜汁松花蛋";

    msg.checked =YES;

    [itemsaddObject:msg];


    msg = [[itemalloc]init];

    msg.text =@"麻辣香锅";

    msg.checked =NO;

    [itemsaddObject:msg];


    msg = [[itemalloc]init];

    msg.text =@"练字!!!";

    msg.checked =NO;

    [itemsaddObject:msg];


    UIBarButtonItem *button = [[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAddtarget:selfaction:@selector(Clicked:)];


    self.navigationItem.rightBarButtonItem = button;

}


- (void) Clicked:(id)sender {


    int newRowIndex = [itemscount];


    item *msg = [[itemalloc]init];

    msg.text =@"老大,我是新来的!";

    msg.checked =NO;

    [itemsaddObject:msg];


    NSIndexPath *indexPath = [NSIndexPathindexPathForRow:newRowIndexinSection:0];

    //此方法需要行切入以及Section.


    NSArray *indexPaths = [NSArrayarrayWithObject:indexPath];

    //打包成数组


    [self.tableViewinsertRowsAtIndexPaths:indexPathswithRowAnimation:UITableViewRowAnimationAutomatic];

    //添加到界面上

}



- (BOOL) tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {


    returnYES;

}


- (BOOL) tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {


    returnYES;

}


- (void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {


    [itemsremoveObjectAtIndex:indexPath.row];


    NSArray *indexPaths = [NSArrayarrayWithObject:indexPath];

    

    [tableView deleteRowsAtIndexPaths:indexPathswithRowAnimation:UITableViewRowAnimationAutomatic];

    //此方法需要传入删除的cell的坐标;

}



- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {


    return [itemscount];

}


- (float) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {


    return50.0f;

}


- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


    staticNSString *cellName =@"Cell";


    UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:cellName];


    if (cell ==nil) {


        cell = [[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:cellName]autorelease];

    }


    item *msg = [itemsobjectAtIndex:indexPath.row];


    [selfconfigureTextForCell:cellwithChecklistItem:msg];

    [selfconfigureCheckmarkForCell:cellwithChecklistItem:msg];


    return cell;

}



//每行的数据赋值

- (void)configureTextForCell:(UITableViewCell *)cell withChecklistItem:(item *)item {


    cell.textLabel.text = item.text;

}




- (void)configureCheckmarkForCell:(UITableViewCell *)cell withChecklistItem:(item *)item {


    if (item.checked) {


        cell.accessoryType =UITableViewCellAccessoryCheckmark;

    } else {


        cell.accessoryType =UITableViewCellAccessoryNone;

    }

}


//选中函数

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {


    UITableViewCell *cell = [tableViewcellForRowAtIndexPath:indexPath];


    item *msg = [itemsobjectAtIndex:indexPath.row];


    [msg toggleChecked];//取反函数


    [selfconfigureCheckmarkForCell:cellwithChecklistItem:msg];

    [tableView deselectRowAtIndexPath:indexPathanimated:YES];

}


- (void)didReceiveMemoryWarning

{

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end

0 0