IOS Programming Editing UITableView, editinguitableview

Source: Internet
Author: User

IOS Programming Editing UITableView, editinguitableview

IOS Programming Editing UITableView

1.1 Editing mode

UITableView has an editing property, and when this property is set to YES, the UITableView enters editing mode.

UITableVIew has an editing property. when the property is set to YES, UITableView is set to editing mode.

Depending on how the table view is configured, the user can change the order of the rows, add rows, or remove rows. editing mode does not allow the user to edit the content of a row.

According to how tableview is configured, you can change the row order, add row, and remove row. However, you cannot Add rows in the editing mode.

That the table view uses the word "header" in two different ways: There can be a table header and there can be section headers. Likewise, there can be a table footer and section footers.

Table view has table header and section header. Similarly, there are table footer and section footer.

Notice that headerView is a strong property. this is because it will be a top-level object in the XIB file; you use weak references for objects that are owned (directly or indirectly) by the top-level objects.

Note that you have used the strong attribute. That's because headerView is the top layer of the xib file. We use weak references to point to objects that belong to the top layer.

 

XIB files are typically used to create the view for a view controller, but they can also be used any time you want to lay out view objects, archive them, and have them loaded at runtime.

The xib file is generally used to create a new view for the view controller. However, they can also be used to get them from whatever you like lay out view object and get them at runtime.

Step 1

Create a new file (Command-N). From the iOS section, select User Interface, choose the Empty template, and click Next

Drag a UIView onto the canvas. then drag two instances of UIButton onto that view. you will then want to resize the UIView so that it just fits the buttons; however, Xcode will not let you: the size is locked. to unlock the size, select the UIView on the canvas and open the attributes inspector. under the Simulated Metrics section, select None for the Size option.

 

To load a XIB file manually, you use NSBundle.

To manually load the XIB file, you need NSBundle.

This class is the interface between an application and the application bundle it lives in.

This class is the interface of application and application bundle.

When you want to access a file in the application bundle, you ask NSBundle for it.

When you need to get a file from application bundle, you need to get it from NSBundle.

An instance of NSBundle is created when your application launches, and you can get a pointer to this instance by sending the message mainBundle to NSBundle.

An NSBundle instance has been created when the application is started. You can send a message mainBundle to NSBundle to obtain a pointer to it.

Once you have a pointer to the main bundle object, you can ask it to load a XIB file.

Once you get the pointer to the main bundle, you can ask it to load an XIB file.

Notice that this is a getter method that does more than just get.

Note that this getter method is not just a get method.

This is a common pattern: Lazy Instantiation puts off creating the object until it is actually needed.

This is a common mode: delayed instantiation. This object is instantiated only when it is actually needed.

In some cases this approach can significantly lower the normal memory footprint of your app.

In some cases, this can indeed reduce your memory.

-(UIView *) headerView

{

// If you have not loaded the headerView yet... if (! _ HeaderView ){

// Load HeaderView. xib

}

[[NSBundle mainBundle] loadNibNamed: @ "HeaderView" owner: self

Options: nil];

Return _ headerView ;}

 

Notice that you passed self as the owner of the XIB file. this ensures that when the main NSBundle is parsing the resultant NIB file at runtime, any connections to the File's Owner placeholder will be made to that BNRItemsViewController instance.

You also noticed that the owner passed self to the xib file. This ensures that when the main NSBundle parses the NIB FIle result, any connection to the FIle's owner placeholder will be in the BNRItemsViewController instance.

 

UIView * view = self. headerView;

[Self. tableView setTableHeaderView: view];

 

In addition, any object can load a XIB file manually by sending the message loadNibNamed: owner: options: to the application bundle.

Any object that wants to load the xib file can send a loadNibNamed: owner: options: to the application bundle.

 

UIViewController's default XIB loading behavior uses the same code.

The only difference is that it connects its view outlet to the view object in the XIB file.

The only difference is that it connects view outlet to view object in the XIB file.

Imagine what the default implementation of loadView for UIViewController probably looks like:

-(Void) loadView

{
// Which bundle is the NIB in?
// Was a bundle passed to initWithNibName: bundle :?

NSBundle * bundle = [self nibBundle]; if (! Bundle ){

// Use the default

Bundle = [NSBundle mainBundle];}

// What is the NIB named?
// Was a name passed to initWithNibName: bundle :? NSString * nibName = [self nibName];
If (! NibName ){

// Use the default

NibName = NSStringFromClass ([self class]);}

// Try to find the NIB in the bundle
NSString * nibPath = [bundle pathForResource: nibName

OfType: @ "nib"];

// Does it exist? If (nibPath ){

// Load it (this will set the view outlet as a side-effect

[Bundle loadNibNamed: nibName owner: self options: nil];} else {

// If there is no NIB, just create a blank UIView

Self. view = [[UIView alloc] init];}

}

 

 

1.1.2 Now let's implement the toggleEditingMode: method.

Now the toogleEditingMode method is implemented.

You cocould toggle the editing property of UITableView directly.

You can directly change the editing property OF UITableView.

However, UITableViewController also has an editing property.

However, UITableViewController still has an editing property.

A UITableViewController instance automatically sets the editing property of its table view to match its own editing property.

The UITableViewController instance automatically sets the editing property attribute of table view to match its own attributes.

To set the editing property for a view controller, you send it the message setEditing: animated:

To set the editing property of the view controller, you can send it a message of setEditing: animated.

-(IBAction) toggleEditingMode :( id) sender

{
// If you are currently in editing mode... if (self. isEditing ){

// Change text of button to inform user of state
[Sender setTitle: @ "Edit" forState: UIControlStateNormal];

// Turn off editing mode

[Self setEditing: NO animated: YES];} else {

// Change text of button to inform user of state
[Sender setTitle: @ "Done" forState: UIControlStateNormal];

// Enter editing mode

[Self setEditing: YES animated: YES];}

}

1.2 Adding rows

There are two common interfaces for adding rows to a table view at runtime.

(1) A button above the cells of the table view.

The button on the cell of table view.

This is usually for adding a record for which there is a detail view.

To add details to a record.

For example, in the Contacts app, you tap a button when you meet a new person and want to take down all their information.

For example, in the contact app, you click a button when you meet a new person and want to record all its information.

(2) A cell with a green plus sign.

A cell with a green plus sign.

This is usually for adding a new field to a record, such as when you want to add a birthday to a person's record in the Contacts app.

 

Ultimately, it is the dataSource of the UITableView that determines the number of rows the table view shocould display.

In the end, the datasource of UITableView determines the number of rows to be displayed.

After inserting a new row, the table view has six rows (the original five plus the new one ).

After adding a row to table view, it becomes six rows.

Then, it runs back to its dataSource and asks it for the number of rows it shoshould be displaying.

Then, the datasource needs to find the number of rows to be displayed.

BNRItemsViewController consults the store and returns that there shoshould be five rows. The UITableView then says, "Hey, that is not right! "And throws an exception.

It will be returned to UITableView that there are five rows. In this case, an error occurs. UITableView is incorrect. An exception is thrown.

You must make sure that the UITableView and its dataSource agree on the number of rows.

Make sure that the number of rows in UITableView is consistent with that in datasource.

Thus, you must add a new BNRItem to the BNRItemStore before you insert the new row.

Therefore, you need to add the new BNRItem to BNRItemStore before adding a new row to UITableView.

-(IBAction) addNewItem :( id) sender {

BNRItem * newItem = [[BNRItemStore sharedStore] createItem];

NSInteger lastRow = [[BNRItemStore sharedStore] allItems] indexOfObject: newItem];

NSIndexPath * indexpath = [NSIndexPath indexPathForRow: lastRow inSection: 0];

[Self. tableView insertRowsAtIndexPaths: @ [indexpath] withRowAnimation: UITableViewRowAnimationTop];

}

 

1.3 Deleting Rows

1.3.1

Before the table view will delete a row, it sends its data source a message about the proposed deletion and waits for a confirmation message before pulling the trigger.

Before table view deletes a row, it sends a message to data source about the deletion proposal and waits for confirmation.

When deleting a cell, you must do two things: remove the row from the UITableView and remove the BNRItem associated with it from the BNRItemStore.

When deleting a cell, you must remove row (2) from UITableView and remove BNRItem from BNRItemStore.

-(Void) removeItem :( BNRItem *) item;

 

-(Void) removeItem :( BNRItem *) item

{
[Self. privateItems removeObjectIdenticalTo: item];

}

1.3.2 removeObject VS removeObjectIdenticalTo

RemoveObject: goes to each object in the array and sends it the message isEqual :. A class can implement this method to return YES or NO based on its own determination. for example, two BNRItem objects cocould be considered equal if they had the same valueInDollars.

RemoveObject: Enter the column to compare objects one by one. Send an isEqual message. A class can implement this method and return yes or no.

The method removeObjectIdenticalTo:, on the other hand, removes an object if and only if it is the exact same object as the one passed in this message.

RemoveObjectIdenticalTo deletes an object only when it is identical with the given object.

1.3.3

You will implement tableView: commitEditingStyle: forRowAtIndexPath:, a method from the UITableViewDataSource protocol.

(This message is sent to the BNRItemsViewController. Keep in mind that while the BNRItemStore is the where the data is kept, the BNRItemsViewController is the table view's dataSource .)

This message is sent to BNRItemsViewController. Remember that when BNRItemStore is the place where data is maintained, BNRItemsViewController is the datasource of table view.

 

When tableView: commitEditingStyle: forRowAtIndexPath: is sent to the data source, two extra arguments are passed along with it. the first is the UITableViewCellEditingStyle, which, in this case, is UITableViewCellEditingStyleDelete. the other argument is the NSIndexPath of the row in the table.

When tableView: commitEditingStyle: forRowAtIndexPath is sent to data source, two additional parameters are passed together. The first is UITableViewCellEditingStyle. In this case, it is UITableViewCellEditingStyleDelete, and the second is the NSIndexPath of the table row.

-(Void) tableView :( UITableView *) tableView commitEditingStyle :( UITableViewCellEditingStyle) editingStyle

ForRowAtIndexPath :( NSIndexPath *) indexPath {

// If the table view is asking to commit a delete command... if (editingStyle = UITableViewCellEditingStyleDelete ){

NSArray * items = [[BNRItemStore sharedStore] allItems]; BNRItem * item = items [indexPath. row];
[[BNRItemStore sharedStore] removeItem: item];

// Also remove that row from the table view with an animation [tableView deleteRowsAtIndexPaths: @ [indexPath]

}}

 

1.4 Moving Rows

Moving a row, however, does not require confirmation; the table view moves the row on its own authority and reports the move to its data source by sending the message tableView: moveRowAtIndexPath: toIndexPath :.

Move a row without confirmation. Table view moves rows in its own domain and sends tableView: moveRowAtIndexPath: toIndexPath to tell the data source to move the rows.

But before you can implement the data source method, you need to give the BNRItemStore a method to change the order of items in its allItems array.

However, before implementing the datasource method, you should give BNRItemStore a method to set the order of items in allItems for a hundred years.

 

In BNRItemStore. h, declare this method.

-(Void) moveItemAtIndex :( NSUInteger) fromIndex toIndex :( NSUInteger) toIndex;

In BNRItemStore. m, implement moveItemAtIndex: toIndex :.
-(Void) moveItemAtIndex :( NSUInteger) fromIndex

ToIndex :( NSUInteger) toIndex

{
If (fromIndex = toIndex ){

Return ;}

// Get pointer to object being moved so you can re-insert it BNRItem * item = self. privateItems [fromIndex];

// Remove item from array
[Self. privateItems removeObjectAtIndex: fromIndex];

// Insert item in array at new location

[Self. privateItems insertObject: item atIndex: toIndex];}

 

In BNRItemsViewController. m, implement tableView: moveRowAtIndexPath: toIndexPath: to update the store.

-(Void) tableView :( UITableView *) tableView moveRowAtIndexPath :( NSIndexPath *) sourceIndexPath

ToIndexPath :( NSIndexPath *) destinationIndexPath {

[[BNRItemStore sharedStore] moveItemAtIndex: sourceIndexPath. row toIndex: destinationIndexPath. row];

}

 

The UITableView can ask its data source at runtime whether it implements tableView: moveRowAtIndexPath: toIndexPath :. if it does, the table view says, "Good, you can handle moving rows. i'll add the re-ordering controls. "If not, it says," If you aren't implementing that method, then I won't put controls there."

UITableView will ask if its data source is Buddha's tableView: moveRowAtIndexPath: toIndexPath. If so, tableview will say, "Well, you can process this moving row. I will add the re-Sort control." If not, "You have not implemented this method, I cannot implement this control. "

 

 

 

 

 

 

 

 

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.