iOS Seventh day (display of 3:uitableview models and grouping of data)

Source: Internet
Author: User

UITableView display of the grouping of models and data

#import "HMViewController.h"#import "HMHero.h"@interfaceHmviewcontroller () <uitableviewdatasource, uitableviewdelegate>@property (nonatomic, strong) UITableView*TableView, @property (nonatomic, strong) Nsarray*heros;@end@implementationHmviewcontroller-(Nsarray *) heros{if(_heros = = nil) _heros =[Hmhero heros]; return_heros;}/** Uitableviewstyleplain,//tablet format uitableviewstylegrouped//grouping format*/-(UITableView *) tableview{if(_tableview = =Nil) {        //a Table control must specify a style when it is created, using only the following instantiation methods_tableview =[[UITableView alloc] InitWithFrame:self.view.bounds Style:uitableviewstyleplain]; _tableview.datasource=Self ; _tableview.Delegate=Self ;    [Self.view Addsubview:_tableview]; }    return_tableview;}- (void) viewdidload{[Super Viewdidload];        [Self tableView]; //Set Row HeightSelf.tableView.rowHeight = the;}#pragmaMark-Data Source methods//The total number of data in each grouping-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{NSLog (@"total number of data per packet%d", Self.heros.count); returnSelf.heros.count;}/** uitableviewcellstyledefault, default type title + optional image UITableViewCellStyleValue1, title + detail + image UITableViewCellStyleValue2, no Display image, Title + Detail Uitableviewcellstylesubtitle Title + detail + image*///tell the table the details of each cell-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{NSLog (@"table row Detail%d", Indexpath.row); UITableViewCell*cell =[[UITableViewCell alloc] Initwithstyle:uitableviewcellstylesubtitle Reuseidentifier:nil]; //Remove Hero ObjectHmhero *hero =Self.heros[indexpath.row]; //titleCell.textLabel.text =Hero.name; //Detailed informationCell.detailTextLabel.text =Hero.intro; //ImageCell.imageView.image =[UIImage ImageNamed:hero.icon]; //set the arrow to the right//1> Uitableviewcellaccessorydisclosureindicator Arrow, you can "prompt" the user, the current line is clickable, usually select the row, will jump to the new page//2> Uitableviewcellaccessorycheckmark check mark, usually prompts the user this row data set up, uses the relatively few//3> Uitableviewcellaccessorydetailbutton button, usually click the button can do independent operations, such as Alertview//Clicking the button does not select the line//4> Uitableviewcellaccessorydetaildisclosurebutton button + arrow, each operation//cell.accessorytype = Uitableviewcellaccessorydetaildisclosurebutton; //Specify a custom view on the right    /** Custom controls are typically used when the type provided by Accessorytype is not satisfied, but you need to add a listener method yourself, usually in a custom cell, not in the View Controller!!!     The event trigger for a custom control also does not affect the selection of the table row! */Uiswitch*switcher =[[Uiswitch alloc] init]; //Adding a listener method[Switcher addtarget:self Action: @selector (switchchanged:) forcontrolevents:uicontroleventvaluechanged]; Cell.accessoryview=switcher; returncell;}- (void) switchchanged: (Uiswitch *) sender{NSLog (@"%s%@", __func__, sender);}#pragmaMark-Proxy method//when Accessorytype is a button, click on the right button's Listening method//This method does not touch the release selection, followed by the row to select their own independent//just for Accessorytype services, not responding to custom controls- (void) TableView: (UITableView *) TableView Accessorybuttontappedforrowwithindexpath: (Nsindexpath *) indexpath{NSLog (@"%s%@", __func__, Indexpath);}//uncheck a row, very little use, very error-prone! //Diddeselectrowatindexpath//Didselectrowatindexpath- (void) TableView: (UITableView *) TableView Diddeselectrowatindexpath: (Nsindexpath *) indexpath{NSLog (@"%s%@", __func__, Indexpath);}//A row is selected with an arrow- (void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) indexpath{NSLog (@"%s%@", __func__, Indexpath);}/** Proxy method priority is higher than RowHeight priority application scenario: Many applications, the height of each row is not the same, for example: Sina Weibo table work observation summary 1> to know how much data-(Nsinteger) TableView: ( UITableView *) TableView numberofrowsinsection: (nsinteger) Section 2> calculates row height for "Each row"-(CGFloat) TableView: (UITableView * ) TableView Heightforrowatindexpath: (Nsindexpath *) Indexpath question: When this method executes, is the cell instantiated?  What is the function of the method? ScrollView need to specify contentsize to be able to scroll, if there is no implementation method, the row height default is 44 need to know the height of each row, to be able to accurately calculate the height of each row contentsize know, naturally know each screen should show how many rows,  The number of execution times of the table detail method The number of rows required to display the 3> table detail call screen, lazy loading, only the table row to display, will be instantiated! Small conclusion: * Tableview.rowheight: High efficiency, suitable for all table rows highly consistent * Proxy method specifies row height: poor efficiency, suitable for each row of the row height is different, can make the table more flexible*/-(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) indexpath{//NSLog (@ "Row height%d", indexpath.row);//        return(Indexpath.row%2) ? -: -; //The following code can be replaced with the RowHeight property! //return;}@end

iOS Seventh day (display of 3:uitableview models and grouping of data)

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.