IOS Table View

Source: Internet
Author: User

A Table View usually needs to set its dataSource and delegate.
You can use Control + drag to set

And adopt two Protocols
@ Interface SecondViewController: UIViewController <UITableViewDelegate, UITableViewDataSource>
UITableViewDelegate is commonly used in the following methods:

// Called after the user changes the selection.- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;// Called before the user changes the selection. Return a new indexPath, or nil, to change the proposed selection.- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath;// Variable height support- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;

UITableViewDataSource is commonly used in the following methods:

@required- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

Note that the above @ required -- these two methods must be implemented, otherwise the program will fail.

How to Set Table View group display?
Implement the following function:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {    NSArray *keys = [[[self artists] allKeys]                      sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];    return [keys objectAtIndex:section];}

Here artists is defined as NSDictionary * artists;
The above function implements grouping by keys in artists.

Implement the following table view style:

1 first, set the Style to grouped.


2. Implement the following functions:

-(NSString *) tableView :( UITableView *) tableView titleForHeaderInSection :( NSInteger) section
Set the title of each section. For example, UIButton and UIButtonTypeRoundedRect are the title of the Section.

-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section
Set the number of rows in each Section. The value is 2.

-(NSInteger) numberOfSectionsInTableView :( UITableView *) tableView
Total number of sections
The above functions are implemented based on the data you actually want to display.

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.