美文网首页
tableview使用

tableview使用

作者: yaya_pangdun | 来源:发表于2016-08-17 22:13 被阅读50次
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];

cell.textLable.text = @"hello";

删除模式

self.tableView.editing = YES; //开启删除模式

左划样式

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //样式定义
    return UITableViewCellEditingStyleInsert;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [self.dataSource removeObjectAtIndex:indexPath.row];
        [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
      //[self.tableView reloadData];
    } else if(editingStyle == UITableVIewCellEditingStyleInsert) {
      [self.listdata insertObject:@"" atIndex:indexPath.row];
      //[self.tableView reloadData];
      NSIndexPath *newPath= [NSIndexPath indexPatForRow:indexPath.row +1 inSection:indexPath.section];
      [self.tableView insertRowsAtIndexPath:@[newPath] withRowAnimation:UITableViewRowAnimationMiddle];
  }
}

移动单元格

-(BOOL) tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    //[self.dataList exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];
    id source = self.dataList[sourceIndexPath.row];
    [self.dataList removeObjectAtIndex:sourceIndexPath.row];
    [self.dataList insertObject:source atIndex:destinationIndexPath.row];
    [self.tableView reloadData];
}

相关文章

网友评论

      本文标题:tableview使用

      本文链接:https://www.haomeiwen.com/subject/qppfsttx.html