1024programmer Blog The use of UITableView for iOS – processing selected cells_allowsselection_little_two_two’s blog

The use of UITableView for iOS – processing selected cells_allowsselection_little_two_two’s blog

1. UITableView attribute

  • allowsSelection: This attribute controls whether the form is allowed to be selected
  • allowsMultipleSelection: This attribute controls whether the form allows multiple selection
  • allowsSelectionDuringEditing: This attribute controls whether the form is allowed to be selected when it is in the editing state
  • allowsMultipleSelectionDuringEditing: This attribute controls whether multiple selection is allowed when the form is in editing state.

2. UITableView method operation attribute

  • indexPathForSelectedRow: Get the NSIndexPath corresponding to the selected table row
  • indexPathsForSelectedRows: Get an array of NSIndexPaths corresponding to all selected table rows
  • selectRowAtIndexPath:animated:scrollPosition:: Control the table to select the table row corresponding to the specified NSIndexPath, and the last parameter controls whether to scroll to the top, middle or bottom of the selected row
  • deselectRowAtIndexPath:animated:: control deselects the table corresponding to the NSIndexPath specified in the tablerow

3. Respond to the selected event – UITableViewDelegate

  • tableView: willSelectRowAtIndexPath:: This method is fired when the user is about to select a row in the table
  • tableView:didSelectRowAtIndexPath:: This method is fired when the user finishes selecting a row in the table
  • tableView:willDeselectRowAtIndexPath:: This method is fired when the user is about to deselect a row in the table
  • tableView: didDeselectRowAtIndexPath:: This method is fired when the user selects a row in the table to cancel

4. Example code – edit the selected line

1) AppDelegate class

//.h
 #import 

 @interface FKAppDelegate : UIResponder 
 @property (strong, nonatomic) UIWindow *window;
 // Two NSArrays to display data as UITableView
 @property (strong, nonatomic) NSMutableArray* books;
 @property (strong, nonatomic) NSMutableArray* details;
 @end



 //.m
 - (BOOL)application:(UIApplication *)application
 didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
 {
 // Create and initialize the NSMutableArray object.
 self.books = [NSMutableArray arrayWithObjects:@"Crazy Android lecture notes",
 @"Crazy iOS handout", @"Crazy Ajax handout", @"Crazy XML handout", nil];
 // Create and initialize the NSMutableArray object.
 self.details = [NSMutableArray arrayWithObjects:
 @"Books that have long been at the top of the sales charts of various online stores",
 @"Comprehensive and detailed iOS development books",
 @"Ajax Development Book",
 @"System introduces XML related knowledge", nil];
 return YES;
 }

2) FKViewController class

//FKViewController.h
 #import 

 @interface FKViewController : UIViewController
 // Bind to the IBOutlet property of the UITableView control on the interface
 @property (strong, nonatomic) IBOutlet UITableView *table;
 @end


 //FKViewController.m
 #import "FKViewController.h"
 #import 
 #import "FKAppDelegate.h"
 #import "FKDetailViewController.h"

 @implementation FKViewController
 // Define the application delegate object
 FKAppDelegate* appDelegate;
 - (void)viewDidLoad
 {
 [super viewDidLoad];
 // Set dataSource and delegate for UITableView control
 self.table.dataSource = self;
 self.table.delegate = self;
 appDelegate = [UIApplication sharedApplication].delegate;
 }
 - (void)viewWillAppear:(BOOL)animated
 {
 [super viewWillAppear:animated];
 [self. table reloadData];
 }
 // The return value of this method determines the control of each table row.
 - (UITableViewCell *)tableView:(UITableView *)tableView
 cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
 // Define a static string as an identifier for the table row
 static NSString* cellId = @"cellId";
 // Take a table row from the queue of reusable table rows
 UITableViewCell* cell = [tableView
 dequeueReusableCellWithIdentifier:cellId];
 // If the fetched table behavior is nil
 if(cell == nil)
 {
 // Create a UITableViewCell object, using the default style
 cell = [[UITableViewCell alloc]
 initWithStyle: UITableViewCellStyleSubtitle
 reuseIdentifier:cellId];
 }
 // Get the row number of the current row from the IndexPath parameter
 NSUInteger rowNo = indexPath.row;
 // Take out the element whose index is rowNo in books as the text title of UITableViewCell
 cell.textLabel.text = [appDelegate.books objectAtIndex:rowNo];
 // Set the border of the cell to rounded corners
 cell.layer.cornerRadius = 12;
 cell.layer.masksToBounds = YES;
 // Set the picture for the left end of UITableViewCell
 cell.imageView.image = [UIImage imageNamed:@"ic_gray.png"];
 // Set the picture for the left end of the UITableViewCell in the highlighted state
 cell.imageView.highlightedImage = [UIImage imageNamed:
 @"ic_highlight.png"];
 // Take out the element whose index is rowNo in details as the detailed content of UITableViewCell
 cell.detailTextLabel.text = [appDelegate.details objectAtIndex:rowNo];
 return cell;
 }
 // The return value of this method determines how many table rows are contained in the specified partition.
 - (NSInteger)tableView:(UITableView*)tableView
 numberOfRowsInSection: (NSInteger) section
 {
 // Since the table has only one partition, directly return the number of collection elements in books to represent the number of rows in the table
 return appDelegate.books.count;
 }
 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:
 (NSIndexPath *)indexPath
 {
 // Get the application delegate object of the application
 FKAppDelegate* appDelegate = [UIApplication
 sharedApplication].delegate;
 // Get the view controller whose ID is detail in the Storyboard file
 FKDetailViewController* detailController = [self. storyboard
 instantiateViewControllerWithIdentifier:@"detail"];
 // Save the NSIndexPath corresponding to the table row the user is editing
 detailController.editingIndexPath = indexPath;
 // Let the application's window display the detailViewController
 appDelegate.window.rootViewController = detailController;
 }
 @end

3) FKDetailViewController class


//.h
 #import 

 @interface FKDetailViewController : UIViewController
 @property (strong, nonatomic) IBOutlet UITextField *nameField;
 @property (strong, nonatomic) IBOutlet UITextField *detailField;
 @property (strong, nonatomic) NSIndexPath* editingIndexPath;
 -(IBAction)clicked:(id)sender;
 - (IBAction)finished:(id)sender;
 @end


 //.m
 #import "FKDetailViewController.h"
 #import "FKViewController.h"
 #import "FKAppDelegate.h"

 @interface FKDetailViewController()

 @end

 @implementation FKDetailViewController
 // Define the application delegate object
 FKAppDelegate* appDelegate;
 // Define the row number of the table row being edited
 NSUInteger rowNo;
 - (void)viewDidLoad
 {
 [super viewDidLoad];
 }
 // This method is called when the view is about to be displayed
 - (void)viewWillAppear:(BOOL)animated
 {
 [super viewWillAppear:animated];
 appDelegate = [UIApplication sharedApplication].delegate;
 // Get the row number of the table row being edited
 rowNo = self.editingIndexPath.row;
 // Assign text to nameField and detailField
 self.nameField.text = [appDelegate.books
 objectAtIndex:rowNo];
 self.detailField.text = [appDelegate.details
 objectAtIndex:rowNo];
 }
 -(IBAction)clicked:(id)sender
 {
 // Replace the element at the specified index in the books collection of appDelegate
 [appDelegate.books replaceObjectAtIndex:rowNo
 withObject:self.nameField.text];
 // Replace the element at the specified index in the details collection of appDelegate
 [appDelegate.details replaceObjectAtIndex:rowNo
 withObject:self.detailField.text];
 // Get the view controller whose ID is list in the Storyboard file
 FKViewController* listController = [self. storyboard
 instantiateViewControllerWithIdentifier:@"list"];
 // The control program window displays the listController controller
 appDelegate.window.rootViewController = listController;
 }
 - (IBAction) finished: (id) sender
 {
 // Tell sender to give up as first responder
 [sender resignFirstResponder];
 }
 @end

You need to add a new ViewController to the storyboard, and associate it with FKDetailViewController, set the ID to detail, associate the original ViewController with FKViewController, and set the ID to list


This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/the-use-of-uitableview-for-ios-processing-selected-cells_allowsselection_little_two_twos-blog/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索