UITableViewController 编辑功能中的,添加,删除,修改

想对UITableViewController进行编辑,得加入编辑按钮,在viewDidLoad中加入下代码

    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    self.navigationItem.rightBarButtonItem.title = GROUPMANAGER;


编辑按钮切换功能时,会调用SetEdit事件,可以重写,如下

[c-sharp]  view plain copy
  1. -(void) setEditing:(BOOL)editing animated:(BOOL)animated  
  2. {  
  3.     [super setEditing:editing animated:animated];  
  4.     self.navigationItem.leftBarButtonItem.enabled = !editing;  
  5.     [self.tableView beginUpdates];  
  6.     NSUInteger count = [groupArray count];  
  7.     NSArray *groupInsertIndexPath = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:count inSection:0]];  
  8.       
  9.     // Add or remove the Add row as appropriate.  
  10.     UITableViewRowAnimation animationStyle = UITableViewRowAnimationNone;  
  11.     if (editing) {  
  12.         self.navigationItem.rightBarButtonItem.title = DoString;  
  13.         if (animated) {  
  14.             animationStyle = UITableViewRowAnimationFade;  
  15.         }  
  16.         [self.tableView insertRowsAtIndexPaths:groupInsertIndexPath withRowAnimation:animationStyle];  
  17.     }  
  18.     else {        
  19.         for (EntityGroup *group in modifyGroupIDArray) {  
  20.             SqliteOperation *sqlOperation = [[SqliteOperation alloc] init];  
  21.             [sqlOperation ModifyGroupName:group];  
  22.             [sqlOperation release];  
  23.         }  
  24.         [modifyGroupIDArray removeAllObjects];  
  25.         self.navigationItem.rightBarButtonItem.title = GROUPMANAGER;  
  26.         [self.tableView deleteRowsAtIndexPaths:groupInsertIndexPath withRowAnimation:UITableViewRowAnimationFade];  
  27.     }  
  28.     [self.tableView endUpdates];      
  29. }  

以下代理方法,设置表中,哪些行,可以进入编辑状态

 

// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    if (indexPath.section == 0) {
        return YES;
    }
    else {
        return NO;
    }
}

 

以下代理方法,设置在编辑状态下,哪些行是删除功能,哪些行是添加功能

-(UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == groupArray.count) {
        return UITableViewCellEditingStyleInsert;
    }
    else {
        return UITableViewCellEditingStyleDelete;
    }    
}

 

以下代理方法,表示执行删除或者添加功能

 

// 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
        EntityGroup *group = [groupArray objectAtIndex:indexPath.row];
        SqliteOperation *sqlOperation = [[SqliteOperation alloc] init];
        [sqlOperation DeleteGroup:group.GroupID];
        [sqlOperation release];
        
        [(NSMutableArray*)groupArray removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        [self addGroup];
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}

 

而修改功能,则需要自定义一个UITableViewCell,其中一个包括UITextField,用于编辑内容

修改内容时完成时,UITextField会执行以下代理方法,前提是将当前的UITextField的delegate设置为代理方法所在类,一般是self

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField

可以将UITextField的tag设置为表的行号,这样就根本tag找到相应的数据源,而修改数据源数据

可以直接在此保存!可以将UITextField,

 

因此在设置表行的代理方法中,需要这样设置

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UIImage *image;
    static NSString *EditCellIdentifier =@"EditCell";
    UITableViewCell *cell = nil;
    cell = [tableView dequeueReusableCellWithIdentifier:EditCellIdentifier];
     if ( cell== nil) {
     cell = [[[NSBundle mainBundle] loadNibNamed:@"ConferencingGroupCell" owner:self options:nil] objectAtIndex:0];          
            }            
     EntityGroup *group = [groupArray objectAtIndex:indexPath.row];

     //自定义cell ConferencingGroupCell
     ConferencingGroupCell *editCell = (ConferencingGroupCell*)cell;
     editCell.txtEdit.text = group.GroupName;
     editCell.txtEdit.delegate = self;
     editCell.lblDiscription.text = [NSString stringWithFormat:@"人数:%d",group.GroupNmuber];            
     editCell.txtEdit.tag = indexPath.row;
     editCell.delegate = self;

     editCell.accessoryType = group.isSelected ? UITableViewCellAccessoryCheckmark:UITableViewCellAccessoryNone;
    return cell;
}

 

在自定义的cell中需要写如下代码,来达到完成编辑时的操作

//表示在编辑或者非编辑状态下,设置UITextField是否可用

- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    // The user can only edit the text field when in editing mode.
    [super setEditing:editing animated:animated];
    txtEdit.enabled = editing;
}
//表示在编辑状态下,出现删除按钮时的代理方法
- (void)willTransitionToState:(UITableViewCellStateMask)state {
    [super willTransitionToState:state];
    
    if (state & UITableViewCellStateEditingMask) {
        lblDiscription.hidden = YES;
        btnDetail.hidden = YES;
    }
}
//表示编辑状态下,删除按钮消失时的代理方法
- (void)didTransitionToState:(UITableViewCellStateMask)state {
    [super didTransitionToState:state];
    
    if (!(state & UITableViewCellStateEditingMask)) {
        lblDiscription.hidden = NO;
        btnDetail.hidden = NO;
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要给tableview表头添加筛选功能,可以使用tableHeaderView属性来自定义表头视图,然后再在表头视图添加筛选控件。 以下是一个示例代码: ```swift class FilterTableHeaderView: UIView { // 筛选控件 let filterControl = UISegmentedControl(items: ["All", "Active", "Completed"]) override init(frame: CGRect) { super.init(frame: frame) // 添加筛选控件 filterControl.selectedSegmentIndex = 0 filterControl.addTarget(self, action: #selector(filterControlValueChanged(_:)), for: .valueChanged) addSubview(filterControl) // 布局筛选控件 filterControl.translatesAutoresizingMaskIntoConstraints = false filterControl.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true filterControl.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } @objc func filterControlValueChanged(_ sender: UISegmentedControl) { // 发送筛选请求 // ... } } class MyTableViewController: UITableViewController { override func viewDidLoad() { super.viewDidLoad() // 创建筛选表头视图 let filterHeaderView = FilterTableHeaderView(frame: CGRect(x: 0, y: 0, width: tableView.bounds.width, height: 44)) tableView.tableHeaderView = filterHeaderView } // ... } ``` 在这个示例,我们创建了一个名为`FilterTableHeaderView`的自定义表头视图,并在其添加了一个`UISegmentedControl`作为筛选控件。我们还为`UISegmentedControl`添加了一个`valueChanged`事件处理程序,当用户更改所选段时,将发送一个筛选请求。最后,在`MyTableViewController`,我们将自定义表头视图设置为表格视图的表头视图。 你可以根据自己的需求修改`FilterTableHeaderView`的筛选控件和事件处理程序,以实现自己的筛选功能

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值