Today Bo Master has a TableView editor needs, encountered some difficulties, here and we share, can progress together.
TableView's edit is through [Self.tableview setediting:bool1 animotion:bool2]; If you need to enter edit mode, call the method and change BOOL1 to Yes. If you want to exit edit mode, The method is called, and BOOL1 is changed to No.bool2 to use animation.
If you want to change the edit button from text to a picture, and you want to replace the text with the Setbackgroudimage method, you will find that the image will be stretched because it cannot be set, and the effect is not satisfactory. Workaround: You can customize the Rightbarbuttonitem, overriding the proxy method. The following will tableview the editor's code to share with you
-(void) Viewdidload {
[Super Viewdidload];
Edit button
Self.navigationItem.rightBarButtonItem = Self.editbuttonitem;
Self.navigationItem.rightBarButtonItem.title = @ "edit";
Self.navigationitem.rightbarbuttonitem.tintcolor=[uicolor Blackcolor];
}
Editor of #pragma mark---------TableView (delete, insert)
Click the Edit button
-(void) setediting: (BOOL) editing animated: (bool) Animated {
[Super Setediting:editing animated:animated];
Don ' t show the back button while editing.
[Self.navigationitem sethidesbackbutton:editing Animated:yes];
if (editing) {
Self.navigationItem.rightBarButtonItem.title = @ "Done";
NSLog (@ "abc");
}else {//Click the Finish button
Self.navigationItem.rightBarButtonItem.title = @ "edit";
NSLog (@ "123");
}
[_customview.tableview setediting:editing Animated:yes];
}
2. Set whether the row in the custom partition (section) can be edited
-(BOOL) TableView: (UITableView *) TableView Caneditrowatindexpath: (Nsindexpath *) Indexpath
{
if (indexpath.section==0) {
return NO;
// }
return YES;
}
3. Set the line (row) editing style for the specified partition (section) (Add, delete)
-(Uitableviewcelleditingstyle) TableView: (UITableView *) TableView Editingstyleforrowatindexpath: (Nsindexpath *) Indexpath
{
if (indexpath.section==1) {
return uitableviewcelleditingstyleinsert;
}
return uitableviewcelleditingstyledelete;
}
4. Edit complete (first manipulate data source, then modify UI)
-(void) TableView: (UITableView *) TableView Commiteditingstyle: (uitableviewcelleditingstyle) Editingstyle Forrowatindexpath: (Nsindexpath *) Indexpath
{
if (editingstyle==uitableviewcelleditingstyledelete) {
NSLog (@ "delete");
Synchronizing updates of modified UI and data between Beginupdates and Endupdates
[_customview.tableview beginupdates];
1. Delete data
NSString *key=_alldatadic.allkeys[indexpath.section];
Nsmutablearray *array=_alldatadic[key];
[Array RemoveObjectAtIndex:indexPath.row];
2. Removing the UI
Delete Row
[_customview.tableview Deleterowsatindexpaths:@[indexpath] withrowanimation:uitableviewrowanimationleft];
if (array.count==0) {
Delete the data on the header (delete the decimal group for the key)
NSString *key=_alldatadic.allkeys[indexpath.section];
[_alldatadic Removeobjectforkey:key];
When there is only one cell left in a partition (or when there is only one data left in the decimal group), delete the zone header
Nsindexset *indexset=[nsindexset IndexSetWithIndex:indexPath.section];
[_customview.tableview Deletesections:indexset Withrowanimation:uitableviewrowanimationleft];
}
[_customview.tableview endupdates];
}else if (Editingstyle==uitableviewcelleditingstyleinsert)
{
NSLog (@ "Insert");
1. Inserting data
NSString *key=_alldatadic.allkeys[indexpath.section];
Nsmutablearray *array=_alldatadic[key];
[Array insertobject:@ "Changping" atIndex:indexPath.row];
2. Insert UI (cell)
Nsindexpath *indexpath1=[nsindexpath indexpathforrow:0 insection:indexpath.section];//Ensure that each insertion of data is in the first row
[_customview.tableview Insertrowsatindexpaths:@[indexpath] withrowanimation:uitableviewrowanimationleft];
}
}
iOS Learning Note 1-tableview Edit