关于UITableView的简单使用


UITableVie是在Apple应用十分广泛的UI组件,在 MVC 模式(Model -View-Control)的界面中共很具有代表性,下面介绍一些很基础的UITableView的知识:


实现UITableView的显示效果,需要加载各行cell,并且赋予数据内容,这需要使VC成为UITableViewDataSource的数据源代理开始,并且用对应的数据模型设置Cell。

主要代码:

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

{

    static NSString *CellIdentifier = @"TableViewDemoVC_CellIndetifier";

    

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    }

    cell.imageView.image = [UIImage imageNamed:@"computerLogo.jpg"];

    cell.textLabel.text = [noteList[indexPath.row] description];

    cell.detailTextLabel.text = @"detailInfo";

    return cell;

}


系统提供的cell的style有三种:

UITableViewCellStyleDefault :无detailTextLabel,设置了内容也不显示

UITableViewCellStyleSubtitle:有detailTextLabel,显示于textLabel下方

UITableViewCellStyleValue1:detailTextLabel,textLabel与accessoryView之间,居右;

UITableViewCellStyleValue2:detailTextLabel,textLabel与accessoryView之间,居左;(该style将使得cell的imageView变为nil,将无法显示图片)

以下是三种cell的截图:



系统提供的cell的accessoryType有五种:

    UITableViewCellAccessoryNone,                             

    UITableViewCellAccessoryDisclosureIndicator,                            

    UITableViewCellAccessoryDetailDisclosureButton,                 

    UITableViewCellAccessoryCheckmark,                                              

    UITableViewCellAccessoryDetailButton 

  (当然你也可以自定义accessoryView通过cell的accessoryView属性



----------------------------------------------------------------------------------------------------------------------------


UITableView 的行操作(换行,点击删除、左滑删除):


// 在表视图单元编辑模式的开启和关闭之间切换

- (void)noteMove:(id)sender

{

    editState = YES; // 标记下: 处于编辑状态

    [self.listView setEditing:!self.listView.editing animated:YES];

}

- (void)noteDelete:(id)sender

{

    editState = NO; // 标记下: 处于普通的查看状态

    [self.listView setEditing:!self.listView.editing animated:YES];

}


通过设置UITableView的编辑状态 [self.listView setEditing:!self.listView.editing animated:YES];,可以实现 删除 和 换行操作

当然,前提需要实现<UITableViewDataSource,UITableViewDelegate>中的若干方法

具体代码如下:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return 1;

}

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

{

    return noteList.count;

}

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

{

    static NSString *CellIdentifier = @"TableViewDemoVC_CellIndetifier";

    

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;


    }

    cell.imageView.image = [UIImage imageNamed:@"computerLogo.jpg"];

    cell.textLabel.text = [noteList[indexPath.row] description];

    cell.detailTextLabel.text = @"detailInfo";

    return cell;

}


// 询问是否可以删除:

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView

          editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

{

    if(!editState)

        return UITableViewCellEditingStyleDelete; // 可以删除

    else

        return UITableViewCellEditingStyleNone;   // 不允许删除

}

// 询问是否可以移动如编辑状态为假则进行删除操作,如果为真则进入编辑状态

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

{

    if(editState)

        return YES;

    else

        return NO;

}

// 移动行 之后 需要怎么处理

-(void)tableView:(UITableView *)tableView

moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath

     toIndexPath:(NSIndexPath *)destinationIndexPath

{

    NSUInteger fromRow = [sourceIndexPath row];     // 旧的 行号

    NSUInteger toRow = [destinationIndexPath row];  // 新的 行号

    id object = [noteList objectAtIndex:fromRow];   // 取除数据

    [noteList removeObjectAtIndex:fromRow];         // 移除

    [noteList insertObject:object atIndex:toRow];   // 添加

}

// 删除行 之后 需要怎么处理

- (void)tableView:(UITableView *)tableView

commitEditingStyle:(UITableViewCellEditingStyle)editingStyle

forRowAtIndexPath:(NSIndexPath *)indexPath

{

    // 取出目标行号

    NSUInteger row = [indexPath row];

    

    // 从数组中移除数据

    [noteList removeObjectAtIndex:row];

    

    // 从列表视图中,以动画的形式,移除行

    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]

                     withRowAnimation:UITableViewRowAnimationFade];

}


补充一些相关信息:


需要修改删除文字,可以 实现 以下方法:

- (nullable NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(nonnull NSIndexPath *)indexPath

{

    return @"删除";

}



























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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值