怎样创建表格---UITableView UITableViewDataSource UITableViewDelegate

创建一个表格框架,在默认生成的代码中进行编辑,下面一一介绍相关的方法:

当运行默认表格时,首先会调用

-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView         //这个方法是控制表格加载时可以加载几个区域,默认值是1,但是可以修改

{

    return  1;

}

之后回调用

- ( NSInteget)tableView:(UITableView*)tableView  numberOfRowsInSection:(NSInteger)section   //这个方法是返回当前表格中有多少行

{

    return 3;

}

然后,表格显示出来

因为继承了NSTableViewDataSource协议,有两个必须实现的方法,为:

当点击增加一行按钮之后,就会回调:

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

{

    static NSString* CellIdentifier = @"Cell";   //单元格有一个重用机制,定义一个静态的字符串对象作为单元格重用的一个唯一标识

    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];   //查看表格中是否有可以使用的单元格,如果没有,则cell为nil

    if(cell == nil)

    {

        cell = [UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:CellIdentifier] autorelease];   //如果没有,则创建一个

    }

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;  //设置单元格右边按钮的显示风格

    cell.terxtLabel.text = @"aaaaaaa";   //设置单元格显示内容

    return cell;

}

 

- (NSInteger)tableView:(UITableView*)tableView  numberOfRowsInSection:(NSInteger)section   //这个方法返回该区域中有几个单元格

{

      return Int;

}

在xcode中创建一个表格应用程序,选择ios下的master-detail Application   创建table

下面是一个刚创建一个表格框架,没有做任何的修改,分析一下代码:

默认情况下,系统会创建三个类,这里不一一细说,看一下tableMasterViewController.m文件

#import "tableMasterViewController.h"

#import "tableDetailViewController.h"

@interface tableMasterViewController () {
    NSMutableArray *_objects;
}
@end

@implementation tableMasterViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = NSLocalizedString(@"Master", @"Master");
    }
    return self;
}
       
- (void)dealloc
{
    [_detailViewController release];
    [_objects release];
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
 // Do any additional setup after loading the view, typically from a nib.

//  设置导航栏,为导航栏增加两个按钮
    self.navigationItem.leftBarButtonItem = self.editButtonItem;

    UIBarButtonItem *addButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)] autorelease];
    self.navigationItem.rightBarButtonItem = addButton;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)insertNewObject:(id)sender     //   当点击增加按钮时,会回调这个方法,为表格增添一行
{
    if (!_objects) {
        _objects = [[NSMutableArray alloc] init];
    }
    [_objects insertObject:[NSDate date] atIndex:0];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

#pragma mark - Table View

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView   //控制表格加载时加载几个区域,默认为1
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  // UITableViewDataSource协议中的方法,必须实现,返回该区域中有
{                                                                                                                                                                                             几个单元格
    return _objects.count;
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath             //生成单元格,加载每一个单元格的时候都会回调这个方{                                                                                                                                                                                                                   法  UITableViewCell是单元格所属的类
    static NSString *CellIdentifier = @"Cell";
   
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }


    NSDate *object = _objects[indexPath.row];
    cell.textLabel.text = [object description];
    return cell;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath      //单元格是否可以编辑
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath  //编辑单元格,删除或插入
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [_objects removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }
}

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath   //移动单元格
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath      //是否可以移动到指定的位置
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath    //点击单元格回调的方法
{
    if (!self.detailViewController) {
        self.detailViewController = [[[tableDetailViewController alloc] initWithNibName:@"tableDetailViewController" bundle:nil] autorelease];
    }
    NSDate *object = _objects[indexPath.row];
    self.detailViewController.detailItem = object;
    [self.navigationController pushViewController:self.detailViewController animated:YES];
}

@end

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值