IOS programming UITableView and UITableViewController, multiple options for iosuitableview

Source: Internet
Author: User

IOS programming UITableView and UITableViewController, multiple options for iosuitableview

IOS programming UITableView and UITableViewController

A UITableView displays a single column of data with a variable number of rows.

UITableView displays the data in a single column and an indefinite number of rows.

Create a new iOS Empty Application project and configure it

1.1 UITableViewController

Model-View-Controller design pattern

(1) Model: Holds data and knows nothing about the user interface.

You have data but do not know the user interface.
(2) View: Is visible to the user and knows nothing about the model objects.

Can be viewed by users, but the model object is unknown.
(3) Controller: Keeps the user interface and the model objects in sync. Controls the flow of the application; for example, the controller might be responsible for showing a "Really delete this item? "Message before actually deleting some data.

Keep the user interface and model object synchronized. For example, the Controller may display "are you sure you want to delete this item" before determining that you want to delete some data ".

 

A UITableView, a view object, does not handle application logic or data.

Therefore, UITableView, a view object, does not know how to process logic and data.

When using a UITableView, you must consider what else is necessary to get the table working in your application:

When you want to use a UITableIView, you must consider what else you need in your application to make the table work.

(1) A UITableView typically needs a view controller to handle its appearance on the screen.

A UITableView generally requires a view controller to process its display part.

(2) A UITableView needs a data source. A UITableView asks its data source for the number of rows

To display, the data to be shown in those rows, and other tidbits that make a UITableView a useful user interface. without a data source, a table view is just an empty container. the dataSource for a UITableView can be any type of Objective-C object as long as it conforms to the UITableViewDataSource protocol.

UITableView requires a data source. A UITableView requires several lines of data source to be displayed. The data to be displayed, and other good things make UITableView a useful user interface. datasource can be any object that complies with UITableViewDataSource protocol.

(3) A UITableView typically needs a delegate that can inform other objects of events involving the UITableView. The delegate can be any object as long as it conforms to the UITableViewDelegate protocol.

UITableVIew generally requires a delegate to notify other objects involving UITableView. Delegate can be any object that complies with the UITableViewDelegate protocol.

 

An instance of the class UITableViewController can fill all three roles: view controller, data source, and delegate.

A UITableViewController instance can be filled with three roles: view controller, data source, and delegate.

UITableViewController is a subclass of UIViewController, so a UITableViewController has a view. A UITableViewController's view is always an instance of UITableView, and the specified handles the preparation and presentation of the UITableView.

UITableViewController is a subclass of UIViewController. Therefore, UITableViewController has a view whose view is UITableView, and UITableViewCOntroller prepares and displays UITableView.

When a UITableViewController creates its view, the dataSource and delegate instance variables of the UITableView are automatically set to point at the UITableViewController

When UITableViewController creates a view, the dataSource and delegate of UITableView automatically point to UITableViewController.

1.2 Subclassing UITableViewController

 

# Import <UIKit/UIKit. h>

@ Interface BNRItemsViewController: UITableViewController

 

The designated initializer of UITableViewController is initWithStyle:, which takes a constant that determines the style of the table view.

The initialization method specified by UITableViewController is initWithStyle. A constant determines the style of the table view.

There are two options:

UITableViewStylePlain and

UITableViewStyleGrouped.

 

You are changing the designated initializer to init. As such, you need to follow the two rules of initializers:

You put the initialization part in the init method. In this way, you have to do two things.

(1) Call the superclass's designated initializer from yours

Call the specified initializer of superclass to initialize your instance.

(2) Override the superclass's designated initializer to call yours

Reproduce the specified initializer of the superclass to call yourself.

-(Instancetype) init
{
// Call the superclass's designated initializer

Self = [super initWithStyle: UITableViewStylePlain];

Return self;

}

-(Instancetype) initWithStyle :( UITableViewStyle) style
{
Return [self init];
}

 

In BNRAppDelegate,

# Import "BNRItemsViewController. h"

// Create a BNRItemsViewController * itemsViewController =

[[BNRItemsViewController alloc] init];

// Place BNRItemsViewController's table view in the window hierarchy self. window. rootViewController = itemsViewController;

1.3 UITableView's Data Source

1.4 Creating BNRItemStore

BNRItemStore will be a singleton.

BNRItemStore will be a singleton.

This means there will only be one instance of this type in the application; if you try to create another instance, the class will quietly return the existing instance

That is to say, there will be only one instance in the application. If you want to create another instance, this class returns an existing instance.

# Import <Foundation/Foundation. h>

@ Interface BNRItemStore: NSObject

// Notice that this is a class method and prefixed with a + instead of-

+ (Instancetype) sharedStore;

@ End

When this message is sent to the BNRItemStore class, the class will check to see if the single instance of BNRItemStore has already been created. if it has, the class will return the instance. if not, it will create the instance and return it.

When this message is sent to BNRItemStore, this class will check whether an instance has been created. If yes, this class will return this instance. If no, it will create and return an instance.

 

@ Implementation BNRItemStore

+ (Instancetype) sharedStore

{
Static BNRItemStore * sharedStore = nil;

// Do I need to create a sharedStore?

If (! SharedStore ){

SharedStore = [[self alloc] initPrivate];}

Return sharedStore ;}

 

// If a programmer CILS [[BNRItemStore alloc] init], let him know the error of his ways

-(Instancetype) init
{

@ Throw [NSException exceptionWithName: @ "Singleton" reason: @ "Use + [BNRItemStore sharedStore]"

Return nil ;{

UserInfo: nil];

 

// Here is the real (secret) initializer

-(Instancetype) initPrivate

{
Self = [super init];

Return self ;}

 

Notice that the variable sharedStore is declared as static. A static variable is not destroyed when the method is done executing. Like a global variable, it is not kept on the stack.

Note that the sharedStore statement is static. After a method is executed, a static variable is not destroyed. Like a global variable, it is not stored in the stack.

@ Class BNRItem;

@ Property (nonatomic, readonly) NSArray * allItems;

-(BNRItem *) createItem;

 

See the @ class directive? That tells the compiler that there is a BNRItem class and that the compiler does not need to know this class's details in the current file-only that it exists.

It tells the compiler that there is such a BNRItem class, and now the compiler does not need to know its details, just need to know that it exists.

Using the @ class directive can speed up compile times considerably because fewer files have to be recompiled when one file changes.

If the file needs to be re-compiled when the file changes, the @ class command may accelerate the compilation. ,

But when we need to know the details in this class, we still need to import it.

@ Interface BNRItemStore ()

@ Property (nonatomic) NSMutableArray * privateItems;

@ End

 

-(BNRItem *) createItem

{
BNRItem * item = [BNRItem randomItem];

[Self. privateItems addObject: item];

Return item ;}

1.5 Implementing data source methods

-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {

Return [[[BNRItemStore sharedStore] allItems] count];

}

 

When Apple started supporting both 32-bit and 64-bit systems, they needed an integer type that was a 32-bit int in 32-bit applications and a 64-bit int in 64-bit applications.

When apple starts to support 32-bit and 64-bit systems, they need 32 and 64 real numbers in 32 and 64-bit machines.

Thus, NSInteger (which is signed) and NSUInteger (which is unsigned) were born. These types are used extensively throughout Apple's frameworks.

Therefore, NSInteger (Signed) and NSUInteger (unsigned) appear.

 

1.6 UITableViewCells

Each row of a table view is a view.

Each row of table view is a view.

These views are instances of UITableViewCell.

These views are an instance of UITableViewCell.

A cell itself has one subview-its contentView

Each cell has a contentView.

The contentView is the superview for the content of the cell.

Each contentView is the superview of the cell content.

The real meat of a specified is the three subviews of the contentView. Two of those subviews are UILabel instances that are properties of UITableViewCell named textLabel and detailTextLabel. The third subview is a UIImageView called imageView

In reality, UITableViewCell is the three child classes of contentView. The UILabel is textLabel and detailTextLabel. The third is that UIImageVIew is called imageView.

Each cell also has a UITableViewCellStyle that determines which subviews are used and their position within the contentView.

Each cell has a UITableViewCellStyle to determine the position of the subview to be used and in the contentView.

 

How do you decide which cell a BNRItem corresponds? One of the parameters sent to tableView: cellForRowAtIndexPath: is an NSIndexPath, which has two properties: section and row. when this message is sent to a data source, the table view is asking, "Can I have a cell to display in section X, row Y? "Because there is only one section in this exercise, your implementation will only be concerned with the row.

When this message is sent to a data source, the table view is asking, "Can I have a cell to display in section X, row Y? "When the message is sent to data source, table view is asking:" Can I have a cell that shows x section and y row ".

1.7 Resuing UITableViewCells

When the user scrolls the table, some cells move offscreen. Offscreen cells are put into a pool of cells available for reuse.

When the user scroll table, some cells are moved to offscreen. Offscreen cells are placed in a reusable cell pool.

Then, instead of creating a brand new cell for every request, the data source first checks the pool.

Instead of creating a new cell for each request, data source first checks the pool

If there is an unused cell, the data source configures it with new data and returns it to the table view.

If there is an unused cell in it, the data source configuration will give it new data and return it to table view.

 

There is one problem: sometimes a UITableView has different types of cells.

Sometimes UITableView has different types of cells.

Occasionally, you have to subclass UITableViewCell to create a special look or behavior.

Sometimes you also need to inherit the UITableView to create a table view with different appearances.

However, different subclasses floating around the pool of reusable cells create the possibility of getting back a cell of the wrong type.

Different subclasses generate

You must be sure of the type of the cell returned to you so that you can be sure of what properties and methods it has.

You must determine the type of the cell to be returned so that you can determine its attributes and methods.

You do not care about getting any specific cell out of the pool because you are going to change the cell content anyway.

You don't care about getting special cells from the pool, because whatever the case, you will change the cell content.

What you need is a cell of a specific type.

What you need is a special cell type.

The good news is that every cell has a reuseIdentifier property of type NSString.

The good news is that each cell has a reuseIdentifier attribute of the NSString type.

When a data source asks the table view for a reusable cell, it passes a string and says, "I need a cell with this reuse identifier ."

When a data source requests a table view reusable cell, it passes a string and says, "I need a cell with such a reuse identifier"

 

// Get a new or recycled cell UITableViewCell * cell =

[TableView dequeueReusableCellWithIdentifier: @ "UITableViewCell" forIndexPath: indexPath];

 

You are giving that control to Apple to get the benefits of the reuse identifier.

You give control to Apple to get the benefits of reuse identifier.

For this to work, you need to tell the table view which kind of cell it showould instantiate if there are no cells in the reuse pool.

To make this work, you need to tell table view that if there is no available cell in the reuse pool, we should select that type of cell for initialization.

In BNRItemsViewController. m, override viewDidLoad to register UITableViewCell class with the table view.

-(Void) viewDidLoad

{
[Super viewDidLoad];

[Self. tableView registerClass: [UITableViewCell class] forCellReuseIdentifier: @ "UITableViewCell"];

}

 

Reusing cells means that you only have to create a handful of cells, which puts fewer demands on memory.

Reusing cells means you only need to create one cell, which requires a small amount of memory.

 

 

 

 

 

 

 

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.