你的位置:首页 > 软件开发 > 操作系统 > iOS 编辑UITableView(根据iOS编程编写)

iOS 编辑UITableView(根据iOS编程编写)

发布时间:2016-09-27 01:00:14
上个项目我们完成了 JXHomepwner 简单的应用展示,项目地址。本节我们需要在上节项目基础上,增加一些响应用户操作。包括添加,删除和移动表格。编辑模式  UITableView 有一个名为 editing 的属性,如果将其设置为 YES , ...

iOS 编辑UITableView(根据iOS编程编写)

  上个项目我们完成了 JXHomepwner 简单的应用展示,项目地址。本节我们需要在上节项目基础上,增加一些响应用户操作。包括添加,删除和移动表格。

  • 编辑模式

  UITableView 有一个名为  editing 的属性,如果将其设置为  YES UITableView 就会进入编辑模式。在编辑模式下,用户可以管理 UITableView 中的表格行,我们可以添加、删除和移动等操作。但是编辑模式没有听过修改行的内容的功能。

  首先要更新界面,使用户可以将 UITableView 对象设置为编辑模式。示例程序是为 UITableView 对象的 表头视图 增加一个按钮,然后通过按钮使  UITableView 对象进入或者退出编辑模式。表头视图是指 UITableView 对象可以在其表格上方显示特定视图,适合放置针对某个表格段或者整张表格的标题和控件。表头视图可以是任意的 UIView 对象。

  表头视图有两种,分别针对表格段和表格。类似的,还有 表尾视图 也具有表格段和表格两种。

  接下来创建一个针对表格的表头视图。这个表头视图包含两个 UIButton 对象,其中一个负责切换 UITableView 对象的编辑模式,另一个负责创建新的 JXItem 对象并加入 UITableView 对象。可以使用代码,也可以使用 XIB 方式来创建。

#import "JXItemsViewController.h"#import "JXItem.h"#import "JXItemStore.h"@interface JXItemsViewController ()/** 头部视图 */@property (nonatomic,weak) UIView * headerView;/** 编辑按钮 */@property (nonatomic,strong) UIButton * editButton;@end@implementation JXItemsViewController- (instancetype)init {  // 调用父类的指定初始化方法  self = [super initWithStyle:UITableViewStylePlain];  if (self) {    for (NSInteger i=0; i<15; i++) {      [[JXItemStore sharedStore] createItem];    }  }  return self;}- (instancetype)initWithStyle:(UITableViewStyle)style {  return [self init];}- (void)viewDidLoad {  [super viewDidLoad];    // 向控制器注册  [self.tableView registerClass:[UITableViewCell class]      forCellReuseIdentifier:@"UITableViewCell"];    // 加载头视图  [self headerView];}#pragma mark - Table view data source- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {  return [[[JXItemStore sharedStore] allItem] count];}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {  // 创建 UITableViewCell 对象,风格使用默认风格  UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"                               forIndexPath:indexPath];  // 获取 allItem 的第 n 个 JXItem 对象  // 然后将该 JXItem 对象的描述信息赋值给 UITableViewCell 对象的 textLabel  // 这里的 n 是该 UITableViewCell 对象所对应的表格索引  NSArray * items = [[JXItemStore sharedStore] allItem];  JXItem * item = items[indexPath.row];    cell.textLabel.text = [item description];  return cell;}#pragma mark - 懒加载- (UIView *)headerView{  if (_headerView == nil) {    UIView * headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 50)];    // 设置头部视图    self.tableView.tableHeaderView = headerView;    headerView.backgroundColor = [UIColor cyanColor];    [headerView addSubview:self.editButton];    _headerView = headerView;  }  return _headerView;}- (UIButton *)editButton{  if (_editButton == nil) {    _editButton = [UIButton buttonWithType:UIButtonTypeCustom];    _editButton.frame = CGRectMake(0, 0, self.view.bounds.size.width / 2, 50);    [_editButton setTitle:@"Edit" forState:UIControlStateNormal];    _editButton.backgroundColor = [UIColor greenColor];    [_editButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];    [_editButton addTarget:self action:@selector(editClick:) forControlEvents:UIControlEventTouchDown];  }  return _editButton;}#pragma mark - 点击事件- (void)editClick:(UIButton *)sender {  if (self.isEditing) { // 如果是编辑状态,取消编辑        // 更改文字    [sender setTitle:@"Edit" forState:UIControlStateNormal];        // 取消编辑    [self setEditing:NO animated:YES];  } else {        // 更改文字    [sender setTitle:@"Done" forState:UIControlStateNormal];        // 开始编辑    [self setEditing:YES animated:YES];  }}@end

原标题:iOS 编辑UITableView(根据iOS编程编写)

关键词:IOS

IOS
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。