UITableViewController 编辑功能中的,添加,删除,修改
2015-02-10 22:44
453 查看
想对UITableViewController进行编辑,得加入编辑按钮,在viewDidLoad中加入下代码
self.navigationItem.rightBarButtonItem = self.editButtonItem;
self.navigationItem.rightBarButtonItem.title = GROUPMANAGER;
编辑按钮切换功能时,会调用SetEdit事件,可以重写,如下
注意:editButtonItem会默认调用方法 setEditing方法, 但是调用的时TableViewController的方法,即为 [self setEditing]。 而不是 [self.tableView setEditing]
[c-sharp] view
plaincopy
-([b]void[/b]) setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
self.navigationItem.leftBarButtonItem.enabled = !editing;
[self.tableView beginUpdates];
NSUInteger count = [groupArray count];
NSArray *groupInsertIndexPath = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:count inSection:0]];
// Add or remove the Add row as appropriate.
UITableViewRowAnimation animationStyle = UITableViewRowAnimationNone;
[b]if[/b] (editing) {
self.navigationItem.rightBarButtonItem.title = DoString;
[b]if[/b] (animated) {
animationStyle = UITableViewRowAnimationFade;
}
[self.tableView insertRowsAtIndexPaths:groupInsertIndexPath withRowAnimation:animationStyle];
}
[b]else[/b] {
[b]for[/b] (EntityGroup *group [b]in[/b] modifyGroupIDArray) {
SqliteOperation *sqlOperation = [[SqliteOperation alloc] init];
[sqlOperation ModifyGroupName:group];
[sqlOperation release];
}
[modifyGroupIDArray removeAllObjects];
self.navigationItem.rightBarButtonItem.title = GROUPMANAGER;
[self.tableView deleteRowsAtIndexPaths:groupInsertIndexPath withRowAnimation:UITableViewRowAnimationFade];
}
[self.tableView endUpdates];
}
以下代理方法,设置表中,哪些行,可以进入编辑状态
// 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;
}
}
以上一定注意
直接在表中,添加一行时,要求能看到效果时,必须先在数据源中添加数据,如下
NSArray *localIndexPath = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:contactArray.count inSection:1]];
[contactArray addObject:contact];
[tableView insertRowsAtIndexPaths:localIndexPath withRowAnimation:UITableViewRowAnimationFade];
直接在表中,删除一行,并且能看到删除效果时,也必须先在数据源中删除相应的数据,如下
NSArray *groupIndexPath = [NSArray arrayWithObject:indexPath];
[(NSMutableArray*)group.ContactArray removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:groupIndexPath withRowAnimation:UITableViewRowAnimationFade];
self.navigationItem.rightBarButtonItem = self.editButtonItem;
self.navigationItem.rightBarButtonItem.title = GROUPMANAGER;
编辑按钮切换功能时,会调用SetEdit事件,可以重写,如下
注意:editButtonItem会默认调用方法 setEditing方法, 但是调用的时TableViewController的方法,即为 [self setEditing]。 而不是 [self.tableView setEditing]
[c-sharp] view
plaincopy
-([b]void[/b]) setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
self.navigationItem.leftBarButtonItem.enabled = !editing;
[self.tableView beginUpdates];
NSUInteger count = [groupArray count];
NSArray *groupInsertIndexPath = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:count inSection:0]];
// Add or remove the Add row as appropriate.
UITableViewRowAnimation animationStyle = UITableViewRowAnimationNone;
[b]if[/b] (editing) {
self.navigationItem.rightBarButtonItem.title = DoString;
[b]if[/b] (animated) {
animationStyle = UITableViewRowAnimationFade;
}
[self.tableView insertRowsAtIndexPaths:groupInsertIndexPath withRowAnimation:animationStyle];
}
[b]else[/b] {
[b]for[/b] (EntityGroup *group [b]in[/b] modifyGroupIDArray) {
SqliteOperation *sqlOperation = [[SqliteOperation alloc] init];
[sqlOperation ModifyGroupName:group];
[sqlOperation release];
}
[modifyGroupIDArray removeAllObjects];
self.navigationItem.rightBarButtonItem.title = GROUPMANAGER;
[self.tableView deleteRowsAtIndexPaths:groupInsertIndexPath withRowAnimation:UITableViewRowAnimationFade];
}
[self.tableView endUpdates];
}
以下代理方法,设置表中,哪些行,可以进入编辑状态
// 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;
}
}
以上一定注意
直接在表中,添加一行时,要求能看到效果时,必须先在数据源中添加数据,如下
NSArray *localIndexPath = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:contactArray.count inSection:1]];
[contactArray addObject:contact];
[tableView insertRowsAtIndexPaths:localIndexPath withRowAnimation:UITableViewRowAnimationFade];
直接在表中,删除一行,并且能看到删除效果时,也必须先在数据源中删除相应的数据,如下
NSArray *groupIndexPath = [NSArray arrayWithObject:indexPath];
[(NSMutableArray*)group.ContactArray removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:groupIndexPath withRowAnimation:UITableViewRowAnimationFade];
相关文章推荐
- UITableViewController 编辑功能中的,添加,删除,修改
- XML---添加 删除、编辑、修改并保存、添加、查找、生成XML按钮并完成对应的功能
- 四.用户管理页面(显示所有的用户信息,同时为用户的添加,编辑,修改密码,删除,修改权限等功能提供相应的链接) UserMange.aspx
- 使用SNMP编辑(添加/删除/修改)ARP表
- GridView动态绑定数据、编辑、修改、删除及分页功能
- MFC:使用SNMP编辑(添加/删除/修改)ARP表
- 一个lucene索引初始化,添加,删除,修改功能的实现
- (VB.net) 利用DataGrid实现查找, 编辑, 修改, 更新, 删除的功能。
- FckEditor添加右键菜单;增加编辑区右键图片删除功能(asp.net )
- js做带编辑,保存,删除,添加功能的表格
- FileSystemObject组件新建读取添加修改删除功能实例
- GridView动态绑定数据、编辑、修改、删除及分页功能
- SupeSite 7.0.0正式版运用在线模板编辑功能修改并添加站点模板
- FileSystemObject组件新建/读取/添加/修改/删除功能实例
- C语言单链表的建立,查找,添加,删除,修改功能实现
- XML中对于一个books.xml的详情显示,删除按钮,修改并保存按钮 和 添加按钮。完成这些按钮所对应的功能(XmlDocument)。
- FckEditor添加右键菜单;增加编辑区右键图片删除功能(asp.net )(一)
- 利用DhtmlXtree实现展现,修改,添加,删除,移动功能一棵树上实现,iframe的单个滑动条显示,包含在iframe中树节点中文内容过长问题解决
- jQuery(4)--实现用户的添加,删除,编辑功能
- 使用XmlDocument将dropdownlist中显示的书名的其它信息读入到相应的文本框中。添加 :删除按钮,修改并保存按钮 和 添加按钮。完成这些按钮所对应的功能(XmlDocument)