Cat Learn iOS (12) UI UITableView Learning (UP) LOL League of Legends exercises

Source: Internet
Author: User

Cat Share, must boutique

Material code Address: http://blog.csdn.net/u013357243/article/details/44706671
Original address: Http://blog.csdn.net/u013357243?viewmode=contents

Look first.



Code for source code Nyviewcontroller
//ps: New iOS Exchange Learning Group: 304570962Can add the cat cat QQ:1764541256Or Znycat Let's study hard together. Original: http://blog.csdn.net/u013357243?viewmode=contents//NYVIEWCONTROLLER.M//06-lol League of Legends////Created by Apple on 15-3-28.//Copyright (c) 2015 Znycat. All rights reserved.//#Import "NYViewController.h"#Import "NYHero.h"@interfaceNyviewcontroller () <UITableViewDataSource,UITableViewDelegate>@property(strong,nonatomic) UITableView *tableview;@property(strong,nonatomic) Nsarray *heros;@end@implementationnyviewcontroller-(Nsarray *) heros{if(_heros = = nil) _heros = [Nyhero heros];return_heros;}/** Lazy Loading TableView * /-(UITableView *) tableview{if(_tableview = = nil) {the//table control must specify a style when it is created        //Uitableviewstyleplain flat format        //uitableviewstylegrouped grouping format_tableview = [[UITableView alloc] InitWithFrame:self.view.bounds Style:uitableviewstyleplain];//Add Data source, join agreement_tableview.datasource = self;/ * There are two ways to set the line height, and the priority of the proxy method is higher than the setrowheight. Application scenarios, many applications, each row height is not the same, for example: Sina Weibo *///_tableview.rowheight = 80;//First type_tableview.delegate = self;///second, to set up an agent,-"protocol-" to implement the proxy method[Self.view Addsubview:_tableview]; }return_tableview;} - (void) viewdidload{[SuperViewdidload]; [Self tableView];} #pragma mark-Data source methods/** The total number of data in each group * /-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (Nsinteger) section{returnSelf.heros.count;}/** tell the table, the details of each cell * /-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{/ * Uitableviewcellstyledefault default type title + optional Image UITableViewCellStyleValue1 Title + detail + image UITableView CellStyleValue2 does not display image, Title + Detail Uitableviewcellstylesubtitle Title + detail + image * /NSLog (@"Table row detail%d", Indexpath.row);//static static variables to ensure that the system allocates only one memory space for the variable in memory    //Static variables, once created, will not be freed, only if the application is destroyed.     StaticNSString *id = @"Cell";//1, go to cache pool to find reusable cellsUITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:id];//2, if you don't find it,    if(cell = = nil) {//NSLog (@ "Instantiate cell");cell = [[UITableViewCell alloc] Initwithstyle:uitableviewcellstylesubtitle Reuseidentifier:id];    Cell.accessorytype = Uitableviewcellaccessorydisclosureindicator; }//Set cell contents//Remove Hero ObjectsNyhero *hero = Self.heros[indexpath.row];//Set title: Hero NameCell.textLabel.text = Hero.name;//settings details: Hero DescriptionCell.detailTextLabel.text = Hero.intro;//Set hero iconCell.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 operation, such as Alertview    //Tap the button does not select the line    //4> Uitableviewcellaccessorydetaildisclosurebutton button + arrow, respective operation//Cell.accessorytype = Uitableviewcellaccessorydetaildisclosurebutton;    //Specify custom view on right    /** typically use a custom control only if 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];//// Add monitoring method//[Switcher Addtarget:self Action: @selector (switchchanged:) forcontrolevents:uicontroleventvaluechanged];//   //Cell.accessoryview = switcher;    returnCell;} -(void) switchchanged: (Uiswitch *) sender{NSLog (@"%s%@", __func__, sender);} #pragma mark-Implement proxy method (row height setting)/** Set line height, higher priority than setrowheight * /-(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) indexpath{return  -;}//Uncheck a row, rarely used, very error-prone! //Diddeselectrowatindexpath//Didselectrowatindexpath- (void) TableView: (UITableView *) TableView Diddeselectrowatindexpath: (Nsindexpath *) indexpath{NSLog (@"%s%@", __func__, Indexpath);}/** selected a line * /-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) indexpath{NSLog (@"%s%@", __func__, Indexpath);}/** Accessorytype for the button, Uitableviewcellaccessorydetailbutton click on the right button's listening method This method does not touch the release selected, with the row selected independent only for the ACCESSORYT Ype Service, does not respond to custom controls * /- (void) TableView: (UITableView *) TableView Accessorybuttontappedforrowwithindexpath: (Nsindexpath *) indexpath{NSLog (@"%s%@", __func__, Indexpath);}@end
Code for the code of the model
////NYHero.h//06-lol League of Legends////Created by Apple on 15-3-28.//Copyright (c) 2015 Znycat. All rights reserved.//#import <Foundation/Foundation.h>  @interface nyhero : nsobject @property(nonatomic, copy)NSString*name;@property(nonatomic, copy)NSString*icon;@property(nonatomic, copy)NSString*intro;-(Instancetype) Initwithdict: (nsdictionary*) dict;+ (instancetype) Herowithdict: (nsdictionary*) dict;+ (Nsarray*) Heros;@end

M implementation file

////NYHERO.M//06-lol League of Legends////Created by Apple on 15-3-28.//Copyright (c) 2015 Znycat. All rights reserved.//#import "NYHero.h"  @implementation nyhero -(Instancetype) Initwithdict: (nsdictionary*) dict{ Self= [SuperINIT];if( Self) {        [ SelfSETVALUESFORKEYSWITHDICTIONARY:DICT]; }return  Self;} + (Instancetype) Herowithdict: (nsdictionary*) dict{return[[ SelfAlloc] initwithdict:dict];} +(Nsarray*) heros{Nsarray*array = [Nsarrayarraywithcontentsoffile:[[NSBundlemainbundle]pathforresource:@"Heros.plist"OfType:Nil]];Nsmutablearray*arraym = [NsmutablearrayArray]; for(nsdictionary*dict in array) {//This is what should be written.[Arraym addobject:[ SelfHerowithdict:dict]]; }returnArraym;}@end
Cat and Cat are two--class method of dictionary model

Cat today committed two, the dictionary initialization method in another unexpectedly so write, not many said, we look at the introduction to see it

 @implementation nyhero -(Instancetype) Initwithdict: (nsdictionary*) dict{ Self= [SuperINIT];if( Self) {        [ SelfSETVALUESFORKEYSWITHDICTIONARY:DICT]; }return  Self;} + (Instancetype) Herowithdict: (nsdictionary*) dict{return[[ SelfAlloc] initwithdict:dict];} +(Nsarray*) heros{Nsarray*array = [Nsarrayarraywithcontentsoffile:[[NSBundlemainbundle]pathforresource:@"Heros.plist"OfType:Nil]];Nsmutablearray*arraym = [NsmutablearrayArray]; for(nsdictionary*dict in array) {//This is what I used to write (the wrong note)        //arraym =[self herowithdict:dict];//This is what should be written.[Arraym addobject:[ SelfHerowithdict:dict]]; }

These two criminals, the key to debug you see, he will have content, but because the name is not too much, it should be heros return value is a bunch of objects composed of arrays, the result you nslog found, he ya return an address value, very strange, at first I thought it was xcode big aunt came ... It turns out that there is no secret in the code.
—————— say this is like the cat wrote the code in the least commented yes ...

Interim summary of Agent mode

Listen to certain events of the control
The proxy mode is used to "decouple" the program directly.

Tables can display very rich data, in order to achieve this result, set the table "Data source".
@required the method that must be implemented.
@optional Optional implementation------------------if the implementation can get special effects, if not implemented, it does not affect the normal operation of the program-can increase the flexibility of the control.

Agent Stage Summary: (How to use)
1, abide by the agreement, pre-defined methods, not implemented, the specific implementation of the agent responsible for the work.
The name of the < control +datasource> the data-related method defined.
The < control's name +delegate> defines an event-related method (typically used to listen for control events).

2, Proxy method:
The 1> method name starts with the name of the control (without the class prefix) and facilitates writing by the programmer to quickly find the required protocol method.
2> The first parameter is yourself, meaning that in the protocol method, you can directly access the properties of an object, or call a method.
The controller sends the data to the control (delegate), the return value of the 3> proxy method.

Cell--uitableviewcell Point of attention

The use of the cache pool (boss Ben, there will be new things updated, but need to understand, see others program time don't know-and easy to understand)

    //1,去缓存池查找可重复用得单元格    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];    //2,如果没找到    ifnil) {//        NSLog(@"实例化单元格");       cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;    }    //3设置单元格内容    //4return cell

You need to specify the cell's style when initializing
Uitableviewcellstyledefault default type title + optional image
UITableViewCellStyleValue1 Title + Detail + image
UITableViewCellStyleValue2 not display image, Title + detail
Uitableviewcellstylesubtitle Title + Detail + image

Usage:

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
Specify the view to the right of the cell

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

   点击按钮并不会选中行

4> Uitableviewcellaccessorydetaildisclosurebutton button + arrow, each operation
Cell.accessorytype = Uitableviewcellaccessorydetaildisclosurebutton;

Specify custom view on the right (example: Select button)

 通常accessoryType提供的类型不能满足时,才会使用自定义控件 但是需要自行添加监听方法,通常用在自定义cell,不要写在视图控制器中!!! 自定义控件的事件触发,同样不会影响表格行的选中!
    UISwitch *switcher = [[UISwitch alloc] init];    //添加监听方法    addTarget:selfaction:@selector(switchChanged:forControlEvents:UIControlEventValueChanged];    cell.accessoryView = switcher;

PS: New iOS Exchange Learning Group: 304570962
You can add a cat qq:1764541256 or Znycat.
Let's study hard together.
Original: Http://blog.csdn.net/u013357243?viewmode=contents

Cat Learn iOS (12) UI UITableView Learning (UP) LOL League of Legends exercises

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.