The table operations described in this article include marking rows, moving rows, deleting rows, and inserting rows.
The project will not be built from scratch this time. The project will be downloaded at http://www.oschina.net/code/snippet_164134_9876. This project is the simplest way to generate a table and write data to it. Open it with xcode 4.2 and perform the above operations on the basis of this project.
1. Mark rows
The Mark row here refers to clicking this row to display a check mark on the right of this row, as shown in:
To implement the tag function, add the code before @ end in viewcontroller. M:
#pragma mark -#pragma mark Table Delegate Methods- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *oneCell = [tableView cellForRowAtIndexPath: indexPath]; if (oneCell.accessoryType == UITableViewCellAccessoryNone) { oneCell.accessoryType = UITableViewCellAccessoryCheckmark; } else oneCell.accessoryType = UITableViewCellAccessoryNone; [tableView deselectRowAtIndexPath:indexPath animated:YES]; }
Code implementation: click a row. If the row is not marked, the row is marked. If the row is marked, the flag is canceled.
The running effect is as follows.
The code above is actually modifying the accessorytype attribute of a row. This attribute can be set to four constants:
UITableViewCellAccessoryCheckmarkUITableViewCellAccessoryDetailDisclosureButtonUITableViewCellAccessoryDisclosureIndicatorUITableViewCellAccessoryNone
The results are shown in sequence:
Uitableviewcellaccessorycheckmark uitableviewcellaccessorydetaildisclosurebutton
Uitableviewcellaccessorydisclosureindicator uitableviewcellaccessorynone
Note: The Blue Circle in the second picture above is not only an icon, but also a control. Clicking it triggers an event. In the previous blog "iOS development 16: I used navigation controller to switch views.
2. Move rows
To move or delete rows, you must start the table editing mode. The setediting: animated: method is used.
2.1 Open viewcontroller. XIB and map the table control to outlet to viewcontroller. H, named mytableview.
2.2 Open viewcontroller. M and add the code at the end of the viewdidload method:
// Start the table editing mode [self. mytableview setediting: Yes animated: Yes];
2.3 Add code before @ end:
// After the edit mode is enabled, red delete buttons are displayed on the left of each line by default. This method is to disable the buttons-(uitableviewcelleditingstyle) tableview :( uitableview *) tableview editingstyleforrowatindexpath :( nsindexpath *) indexpath {return rows;} // This method is used to tell the table whether this row can be moved-(bool) tableview :( uitableview *) tableview canmoverowatindexpath :( nsindexpath *) indexpath {return yes;} // This method is used to perform the move operation-(void) tableview :( uitableview *) tableview moverowatindexpath :( nsindexpath *) sourceindexpath toindexpath :( nsindexpath *) destinationindexpath {nsuinteger fromrow = [sourceindexpath row]; nsuinteger torow = [destinationindexpath row]; Id object = [list objectatindex: fromrow]; [list removeobjectatindex: fromrow]; [list insertobject: object atindex: torow];}
The constant uitableviewcelleditingstylenone is used in the editingstyleforrowatindexpath method, which indicates that it cannot be edited. The editing here refers to deletion and insertion. Constants that indicate the editing mode of table rows include:
UITableViewCellEditingStyleDeleteUITableViewCellEditingStyleInsertUITableViewCellEditingStyleNone
As the name suggests, the first one indicates deletion, the second one indicates insertion, and the third one indicates that the deletion is not editable.
If you change the value of uitableviewcelleditingstylenone in the editingstyleforrowatindexpath Method to the preceding values, the running effect is shown in sequence:
2.4 run. You can see that the row is moved:
However, you will also find that you cannot mark each line. This indicates that the row cannot be selected in editing mode, so the didselectrowatindexpath method is not executed.
3. Delete rows
To delete a row from step 1 is actually relatively simple.
3.1 modify uitableviewcelleditingstylenone in the editingstyleforrowatindexpath method to uitableviewcelleditingstyledelete.
3.2 Add code before @ end:
// This method deletes or inserts the value of-(void) tableview (uitableview *) tableview commiteditingstyle: (uitableviewcelleditingstyle) editingstyle forrowatindexpath :( nsindexpath *) Based on the parameter editingstyle *) indexpath {nsuinteger ROW = [indexpath row]; If (editingstyle = uitableviewcelleditingstyledelete) {[self. list removeobjectatindex: Row]; [tableview deleterowsatindexpaths: [nsarray arraywithobject: indexpath] withrowanimation: uitableviewrowanimationautomatic];}
In this method, another constant uitableviewrowanimationautomatic is displayed, which indicates the effect of deletion. Similar constants include:
UITableViewRowAnimationAutomaticUITableViewRowAnimationTopUITableViewRowAnimationBottomUITableViewRowAnimationLeftUITableViewRowAnimationRightUITableViewRowAnimationMiddleUITableViewRowAnimationFadeUITableViewRowAnimationNone
Their effects will not be described one by one. You can try them in actual use.
3.3 run and check the effect:
The image shown on the left is displayed when you just run it. Click the circle icon on the left of a line to display it, as shown in the middle picture. Click the delegate button to delete the row, as shown in the image on the right.
4. Insert rows
This is similar to deleting a row.
4.1 first, modify uitableviewcelleditingstyledelete in the editingstyleforrowatindexpath method to uitableviewcelleditingstyleinsert.
4.2 Add code to the method added in 3.2:
Else {// we insert a row at the position of the selected row. Therefore, the indexpath nsarray * insertindexpaths = [nsarray arraywithobjects: indexpath, nil] is directly used. // Similarly, add data to the list and use row [self. list insertobject: @ "newly added row" atindex: Row]; [tableview insertrowsatindexpaths: insertindexpaths withrowanimation: uitableviewrowanimationright];}
In the above code, you can also use the [tableview reloaddata]; statement without using the insertrowsatindexpaths method.
4.3. Run the following command:
As shown in the preceding figure, when a plus sign is clicked, a new row will fly in from the right side, because the uitableviewrowanimationright parameter is used in insertrowsatindexpaths.
How to remove the default split line between each cell in uitableview
Simple, just
Tableview. separatorstyle = no;
The project will not be built from scratch this time. The project will be downloaded at http://www.oschina.net/code/snippet_164134_9876. This project is the simplest way to generate a table and write data to it. Open it with xcode 4.2 and perform the above operations on the basis of this project.
1. Mark rows
The Mark row here refers to clicking this row to display a check mark on the right of this row, as shown in:
To implement the tag function, add the code before @ end in viewcontroller. M:
#pragma mark -#pragma mark Table Delegate Methods- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *oneCell = [tableView cellForRowAtIndexPath: indexPath]; if (oneCell.accessoryType == UITableViewCellAccessoryNone) { oneCell.accessoryType = UITableViewCellAccessoryCheckmark; } else oneCell.accessoryType = UITableViewCellAccessoryNone; [tableView deselectRowAtIndexPath:indexPath animated:YES]; }
Code implementation: click a row. If the row is not marked, the row is marked. If the row is marked, the flag is canceled.
The running effect is as follows.
The code above is actually modifying the accessorytype attribute of a row. This attribute can be set to four constants:
UITableViewCellAccessoryCheckmarkUITableViewCellAccessoryDetailDisclosureButtonUITableViewCellAccessoryDisclosureIndicatorUITableViewCellAccessoryNone
The results are shown in sequence:
Uitableviewcellaccessorycheckmark uitableviewcellaccessorydetaildisclosurebutton
Uitableviewcellaccessorydisclosureindicator uitableviewcellaccessorynone
Note: The Blue Circle in the second picture above is not only an icon, but also a control. Clicking it triggers an event. In the previous blog "iOS development 16: I used navigation controller to switch views.
2. Move rows
To move or delete rows, you must start the table editing mode. The setediting: animated: method is used.
2.1 Open viewcontroller. XIB and map the table control to outlet to viewcontroller. H, named mytableview.
2.2 Open viewcontroller. M and add the code at the end of the viewdidload method:
// Start the table editing mode [self. mytableview setediting: Yes animated: Yes];
2.3 Add code before @ end:
// After the edit mode is enabled, red delete buttons are displayed on the left of each line by default. This method is to disable the buttons-(uitableviewcelleditingstyle) tableview :( uitableview *) tableview editingstyleforrowatindexpath :( nsindexpath *) indexpath {return rows;} // This method is used to tell the table whether this row can be moved-(bool) tableview :( uitableview *) tableview canmoverowatindexpath :( nsindexpath *) indexpath {return yes;} // This method is used to perform the move operation-(void) tableview :( uitableview *) tableview moverowatindexpath :( nsindexpath *) sourceindexpath toindexpath :( nsindexpath *) destinationindexpath {nsuinteger fromrow = [sourceindexpath row]; nsuinteger torow = [destinationindexpath row]; Id object = [list objectatindex: fromrow]; [list removeobjectatindex: fromrow]; [list insertobject: object atindex: torow];}
The constant uitableviewcelleditingstylenone is used in the editingstyleforrowatindexpath method, which indicates that it cannot be edited. The editing here refers to deletion and insertion. Constants that indicate the editing mode of table rows include:
UITableViewCellEditingStyleDeleteUITableViewCellEditingStyleInsertUITableViewCellEditingStyleNone
As the name suggests, the first one indicates deletion, the second one indicates insertion, and the third one indicates that the deletion is not editable.
If you change the value of uitableviewcelleditingstylenone in the editingstyleforrowatindexpath Method to the preceding values, the running effect is shown in sequence:
2.4 run. You can see that the row is moved:
However, you will also find that you cannot mark each line. This indicates that the row cannot be selected in editing mode, so the didselectrowatindexpath method is not executed.
3. Delete rows
To delete a row from step 1 is actually relatively simple.
3.1 modify uitableviewcelleditingstylenone in the editingstyleforrowatindexpath method to uitableviewcelleditingstyledelete.
3.2 Add code before @ end:
// This method deletes or inserts the value of-(void) tableview (uitableview *) tableview commiteditingstyle: (uitableviewcelleditingstyle) editingstyle forrowatindexpath :( nsindexpath *) Based on the parameter editingstyle *) indexpath {nsuinteger ROW = [indexpath row]; If (editingstyle = uitableviewcelleditingstyledelete) {[self. list removeobjectatindex: Row]; [tableview deleterowsatindexpaths: [nsarray arraywithobject: indexpath] withrowanimation: uitableviewrowanimationautomatic];}
In this method, another constant uitableviewrowanimationautomatic is displayed, which indicates the effect of deletion. Similar constants include:
UITableViewRowAnimationAutomaticUITableViewRowAnimationTopUITableViewRowAnimationBottomUITableViewRowAnimationLeftUITableViewRowAnimationRightUITableViewRowAnimationMiddleUITableViewRowAnimationFadeUITableViewRowAnimationNone
Their effects will not be described one by one. You can try them in actual use.
3.3 run and check the effect:
The image shown on the left is displayed when you just run it. Click the circle icon on the left of a line to display it, as shown in the middle picture. Click the delegate button to delete the row, as shown in the image on the right.
4. Insert rows
This is similar to deleting a row.
4.1 first, modify uitableviewcelleditingstyledelete in the editingstyleforrowatindexpath method to uitableviewcelleditingstyleinsert.
4.2 Add code to the method added in 3.2:
Else {// we insert a row at the position of the selected row. Therefore, the indexpath nsarray * insertindexpaths = [nsarray arraywithobjects: indexpath, nil] is directly used. // Similarly, add data to the list and use row [self. list insertobject: @ "newly added row" atindex: Row]; [tableview insertrowsatindexpaths: insertindexpaths withrowanimation: uitableviewrowanimationright];}
In the above code, you can also use the [tableview reloaddata]; statement without using the insertrowsatindexpaths method.
4.3. Run the following command:
As shown in the preceding figure, when a plus sign is clicked, a new row will fly in from the right side, because the uitableviewrowanimationright parameter is used in insertrowsatindexpaths.
How to remove the default split line between each cell in uitableview
Simple, just
Tableview. separatorstyle = no;