表格视图控制器UITableViewController

这里写图片描述

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    [self.window makeKeyAndVisible];

    UINavigationController* nav = [[UINavigationController alloc]initWithRootViewController:[[LGG_TableViewController alloc]init]];
    self.window.rootViewController = nav;
    return YES;
}
@property (retain)NSMutableArray* datSource;

@end

@implementation LGG_TableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _datSource = [NSMutableArray new];
    //添加数据源数据
    for (NSInteger i = 0; i < 10; i++) {
        NSString* content = [NSString stringWithFormat:@"第%ld行数据",(long)i];
        [_datSource addObject:content];
    }

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.

    //self.editButtonItem 这是表格控制器自带的编辑按钮,用来控制表格编辑状态的开启和关闭
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

#pragma mark - Table view data source

//表格控制器自带表格视图,也自动遵守了表格的两个协议<UITableViewDelegate, UITableViewDataSource>,不需要我们自己去遵守协议设置代理

//返回表格视图分区的数量,默认样式的表格视图可以不实现这个协议方法,分组风格的表格必须实现,默认风格样式的表格这个方法如果要实现只要返回1就行
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return 1;
}

//返回每个分区有多少行数据,这个方法是默认样式和分组样式风格的表格视图都必须实现的,这个方法有多少个分区就会调用多少次
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    //返回数组的长度
    return _datSource.count;
}

//UITableViewCell 单元格,这个方法是告诉表格每行显示什么样的单元格,单元格上显示哪些内容,内容包括标题,图片,挂件等
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //创建一个静态的字符串,当做单元格的标记
    static NSString* cellID = @"cell";

    //向表格视图的单元格池中获取有该标记的单元格,获取到的单元格是暂时没有被使用的
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    //创建单元格,第一个参数是单元格的类型,第二个参数是单元格的标记,如果不使用复用机制第二个参数可以给nul
    //UITableViewCellStyleDefault 默认
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
    }
    //设置单元格的标题
    cell.textLabel.text = _datSource[indexPath.row];
    //设置单元格的副标题
    cell.detailTextLabel.text = _datSource[indexPath.row];
    // Configure the cell...

    return cell;
}


// Override to support conditional editing of the table view.
//告诉表格当前行是否可以编辑,YES代表可以编辑,NO代表不能编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    //indexPath 实际上是单元格的索引,包括当前单元所处的分区索引和行的索引
    //indexPath.section 单元格分区索引
    //indexPath.row 单元格行的索引
    NSLog(@"%ld",(long)indexPath.row);
    if (indexPath.row%2 == 0) {
        return NO;
    }
    return YES;
}



// Override to support editing the table view.
//表格删除
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        //删除相应行的数据源数据
        [_datSource removeObjectAtIndex:indexPath.row];

        //删除有动画
//        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        //让表格视图重新加载数据
        //[tableView reloadData];
    } 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
    }  
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值