Delete, move, and insert IOS table View cells
-(Void) viewDidLoad
{
[Super viewDidLoad];
// Set the navigation bar
Self. editButtonItem. title = @ "edit ";
Self. navigation. rightBarButtonItem = self. editButtonItem;
[Self initTableViewData];
// Do any additional setup after loading the view.
}
-(Void) didReceiveMemoryWarning
{
[Super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(Void) initTableViewData {
NSBundle * bundle = [NSBundle mainBundle];
NSString * plistPath = [bundle pathForResource: @ "user_head" ofType: @ "plist"];
DataArr = [[NSMutableArray alloc] initWithContentsOfFile: plistPath];
}
-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section
{
Return [dataArr count];
}
-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath
{
Static NSString * CellIdentifier = @ "tableCell ";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];
NSUInteger row = [indexPath row];
NSDictionary * rowDict = [dataArr objectAtIndex: row];
Cell. textLabel. text = [rowDict objectForKey: @ "itemName"];
NSLog (@ "cell. label. text = % @", [rowDict objectForKey: @ "itemName"]);
NSString * imagePath = [rowDict objectForKey: @ "itemImagePath"];
Cell. imageView. image = [UIImage imageNamed: imagePath];
NSLog (@ "cell. image. image = % @", imagePath );
Cell. accessoryType = UITableViewCellAccessoryDisclosureIndicator;
Return cell;
}
// Select the Cell to respond to the event
-(Void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath {
[TableView deselectRowAtIndexPath: indexPath animated: YES]; // The selected reversed color disappears immediately.
NSUInteger row = [indexPath row];
NSDictionary * rowDict = [dataArr objectAtIndex: row];
NSString * userName = [rowDict objectForKey: @ "itemName"];
NSLog (@ "userName = % @", userName );
}
// Return the edited style
-(UITableViewCellEditingStyle) tableView :( UITableView *) tableView
EditingStyleForRowAtIndexPath :( NSIndexPath *) indexPath
{
// UITableViewCellEditingStyleInsert
// Return UITableViewCellEditingStyleNone;
Return UITableViewCellEditingStyleDelete;
}
// Trigger event for completion of Editing
-(Void) tableView :( UITableView *) tableView commitEditingStyle :( UITableViewCellEditingStyle) editingStyle
ForRowAtIndexPath :( NSIndexPath *) indexPath
{
If (editingStyle = UITableViewCellEditingStyleDelete)
{
[DataArr removeObjectAtIndex: indexPath. row];
// [TableView insertRowsAtIndexPaths: [NSArray arrayWithObject: indexPath]
// WithRowAnimation: UITableViewRowAnimationFade];
[TableView deleteRowsAtIndexPaths: [NSArray arrayWithObject: indexPath]
WithRowAnimation: UITableViewRowAnimationFade];
[TableView reloadData];
}
}
// UIViewController lifecycle method, used to respond to view editing status changes
-(Void) setEditing :( BOOL) editing animated :( BOOL) animated {
[Super setEditing: editing animated: animated];
[Self. tableView setEditing: editing animated: YES];
If (self. editing ){
Self. editButtonItem. title = @ "done ";
} Else {
Self. editButtonItem. title = @ "edit ";
}
}
@ End
-(Void) viewDidLoad {[super viewDidLoad]; // set the self in the navigation bar. editButtonItem. title = @ "edit"; self. navigation. rightBarButtonItem = self. editButtonItem; [self initTableViewData]; // Do any additional setup after loading the view .} -(void) didReceiveMemoryWarning {[super didreceivemorywarning]; // Dispose of any resources that can be recreated .} -(void) initTableViewData {NSBundle * bundle = [NSBundle mainBundle]; NSString * plistPath = [bundle pathForResource: @ "user_head" ofType: @ "plist"]; dataArr = [[NSMutableArray alloc] initWithContentsOfFile: plistPath];}-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {return [dataArr count];} -(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {static NSString * CellIdentifier = @ "tableCell"; UITableViewCell * cell = [tableView progress: complete]; NSUInteger row = [indexPath row]; NSDictionary * rowDict = [dataArr objectAtIndex: row]; cell. textLabel. text = [rowDict objectForKey: @ "itemName"]; NSLog (@ "cell. label. text = % @ ", [rowDict objectForKey: @" itemName "]); NSString * imagePath = [rowDict objectForKey: @" itemImagePath "]; cell. imageView. image = [UIImage imageNamed: imagePath]; NSLog (@ "cell. image. image = % @ ", imagePath); cell. accessoryType = response; return cell;} // select the Cell response event-(void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath {[tableView deselectRowAtIndexPath: indexPath animated: YES]; // The reversed color after selection instantly disappears NSUInteger row = [indexPath row]; NSDictionary * rowDict = [dataArr objectAtIndex: row]; NSString * userName = [rowDict objectForKey: @ "itemName"]; NSLog (@ "userName = % @", userName);} // return the edited style-(UITableViewCellEditingStyle) tableView :( UITableView *) tableView editingStyleForRowAtIndexPath :( NSIndexPath *) indexPath {// UITableViewCellEditingStyleInsert // return success;} // The event triggered when editing is completed-(void) tableView :( UITableView *) tableView commitEditingStyle :( UITableViewCellEditingStyle) editingStyleforRowAtIndexPath :( NSIndexPath *) indexPath {if (editingStyle = UITableViewCellEditingStyleDelete) {[dataArr removeObjectAtIndex: indexPath. row]; // [tableView attributes: [NSArray arrayWithObject: indexPath] // withRowAnimation: parent]; [tableView attributes: [NSArray arrayWithObject: indexPath] withRowAnimation: parent]; [tableView reloadData] ;}// UIViewController lifecycle method, used to respond to view editing status changes-(void) setEditing :( BOOL) editing animated :( BOOL) animated {[super setEditing: editing animated: animated]; [self. tableView setEditing: editing animated: YES]; if (self. editing) {self. editButtonItem. title = @ "complete";} else {self. editButtonItem. title = @ "edit" ;}}@ end
2. Move Cells
// Trigger event to complete the move. Do not add this method and do not implement the move function.
-(Void) tableView :( UITableView *) tableView moveRowAtIndexPath :( NSIndexPath *) sourceIndexPath
ToIndexPath :( NSIndexPath *) destinationIndexPath
{
NSDictionary * item = [dataArr objectAtIndex: sourceIndexPath. row];
[DataArr removeObjectAtIndex: sourceIndexPath. row];
[DataArr insertObject: item atIndex: destinationIndexPath. row];
}
// Trigger event for mobile completion. If this method is not added, the mobile function is not implemented-(void) tableView :( UITableView *) tableView moveRowAtIndexPath :( NSIndexPath *) sourceIndexPath toIndexPath :( NSIndexPath *) destinationIndexPath {NSDictionary * item = [dataArr objectAtIndex: sourceIndexPath. row]; [dataArr removeObjectAtIndex: sourceIndexPath. row]; [dataArr insertObject: item atIndex: destinationIndexPath. row];}
3. Add cells. The following is a custom trigger event, that is, click the add button in the lower left corner.
Cpp Code
-(IBAction) addistItem :( UIBarButtonItem *) sender {
AppUtils * appUtils = [AppUtils alloc];
// Initialize a UIAlertView first.
UIAlertView * alert = [UIAlertView alloc];
[AppUtils showInputDialogWithTitle: @ "add" message: @ "please input new user name:" toAlertView: alert confirmAction :( ^ {
// Obtain the input box
UITextField * textField = [alert textFieldAtIndex: 0];
// Do not write it as NSMutableDictionary * newItem = [NSDictionary dictionary];
NSMutableDictionary * newItem = [NSMutableDictionary dictionary];
[NewItem setObject: textField. text forKey: @ "itemName"];
[NewItem setObject: @ "1.jpeg" forKey: @ "itemImagePath"];
[DataArr addObject: newItem];
[Self. tableView reloadData];
})];
}
-(IBAction) addistItem :( optional *) sender {AppUtils * appUtils = [AppUtils alloc]; // You Need to initialize a UIAlertView * alert = [UIAlertView alloc]; [appUtils failed: @ "add" message: @ "please input new user name:" toAlertView: alert confirmAction :( ^ {// get the input box UITextField * textField = [alert textFieldAtIndex: 0]; // do not write NSMutableDictionary * newItem = [NSDictionary dictionary]; NSMutableDictionary * newItem = [NSMutableDictionary dictionary]; [newItem setObject: textField. text forKey: @ "itemName"]; [newItem setObject: @ "1.jpeg" forKey: @ "itemImagePath"]; [dataArr addObject: newItem]; [self. tableView reloadData];})];}
4. AppUtils class attached
Cpp Code
# Import "AppUtils. h"
# Include "RIButtonItem. h"
# Include "UIAlertView + Blocks. h"
@ Implementation AppUtils
// The trigger event that pops up the alarm box and implements the alarm box button
-(Void) showInputDialogWithTitle :( NSString *) title message :( NSString *) message toAlertView :( UIAlertView *) alert confirmAction :( void (^) (void) action {
RIButtonItem * cancelItem = [RIButtonItem item];
CancelItem. label = @ "No ";
CancelItem. action = ^
{
// Processing when the value is NO
UITextField * tf = [alert textFieldAtIndex: 0];
NSLog (@ "UITextField = % @", tf. text );
};
RIButtonItem * confirmItem = [RIButtonItem item];
ConfirmItem. label = @ "Yes ";
// ConfirmItem. action = action;
Alert = [alert initWithTitle: title
Message: message
CancelButtonItem: cancelItem
OtherButtonItems: confirmItem, nil];
Alert. alertViewStyle = UIAlertViewStylePlainTextInput;
ConfirmItem. action = action;
[Alert show];
}
@ End
# Import "AppUtils. h "# include" RIButtonItem. h "# include" UIAlertView + Blocks. h "@ implementation AppUtils // a warning box is displayed, and the trigger event (void) showInputDialogWithTitle :( NSString *) title message :( NSString *) message toAlertView :( UIAlertView *) is implemented *) alert confirmAction :( void (^) (void) action {RIButtonItem * cancelItem = [RIButtonItem item]; cancelItem. label = @ "No"; cancelItem. UITextField * tf = [alert textFieldAtIndex: 0]; NSLog (@ "UITextField = % @", tf. text) ;}; RIButtonItem * confirmItem = [RIButtonItem item]; confirmItem. label = @ "Yes"; // confirmItem. action = action; alert = [alert initWithTitle: title message: message cancelButtonItem: cancelItem otherButtonItems: confirmItem, nil]; alert. alertViewStyle = UIAlertViewStylePlainTextInput; confirmItem. action = action; [alert show];} @ end