天天看点

IOS 开发使用UITableView 实现滑动 删除等多个按钮

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return  _addressArray.count;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50.0;
}


-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    
    return nil;
}


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 5;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSUInteger row = [indexPath row];
    NSString *cellIdentifier = [NSString stringWithFormat:@"%lu", (unsigned long)row];
    
    
    self.nearbyCell = (NearbyTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (self.nearbyCell == nil)
    {
        self.nearbyCell = [[NearbyTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
                                                reuseIdentifier:cellIdentifier];
    }
    [self.nearbyCell setContronlsValue:_addressArray[row] :_iphoneArray[row]];
    _nearbyCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return self.nearbyCell;
}


-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self performSelector:@selector(deleselect) withObject:nil afterDelay:0.5f];
}



- (void) deleselect {
    [self.addressTableView deselectRowAtIndexPath:[self.addressTableView indexPathForSelectedRow] animated:YES];
}




//滑动删除 cell
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}


- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{

    return UITableViewCellEditingStyleDelete;

}



- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (editingStyle ==UITableViewCellEditingStyleDelete)
    {
        [self.addressArray removeObjectAtIndex:indexPath.row];
        [self.iphoneArray removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        
//        [tableView deleteSections:<#(nonnull NSIndexSet *)#> withRowAnimation:UITableViewRowAnimationFade];
    }

}


//- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    return @"删除";
//}



- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UITableViewRowAction *topRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"置顶" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        
                NSLog(@"点击了置顶");
        
                // 1. 更新数据
                // 2. 更新UI
        
                NSIndexPath *firstIndexPath = [NSIndexPath indexPathForRow:0 inSection:indexPath.section];
                [tableView moveRowAtIndexPath:indexPath toIndexPath:firstIndexPath];
            }];
            topRowAction.backgroundColor = [UIColor redColor];
    
    
    UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        
        NSLog(@"点击了置顶");
        
        // 1. 更新数据
        // 2. 更新UI
        
        NSIndexPath *firstIndexPath = [NSIndexPath indexPathForRow:0 inSection:indexPath.section];
        [tableView moveRowAtIndexPath:indexPath toIndexPath:firstIndexPath];
    }];
    deleteRowAction.backgroundColor = [UIColor grayColor];
    
     return @[topRowAction,deleteRowAction];
}