iOS Learning-Tableviewcell (group purchase)

Source: Internet
Author: User

model////yjtg.h//01-tableviewcell-buy////Created by Jacky-mac on 15-6-29.//Copyright (c) 2015 www.train.com. All rights reserved.//#import <Foundation/Foundation.h> @interface yjtg:nsobject@property (nonatomic,copy) NSString *icon; @property (nonatomic,copy) nsstring *price; @property (nonatomic,copy) nsstring *title; @property ( nonatomic,copy) NSString *buycount;-(instancetype) initwithdict: (Nsdictionary *) dict;+ (instancetype) TgWithDict: ( Nsdictionary *) dict;+ (Nsmutablearray *) TGS; @end////yjtg.m//01-tableviewcell-Group purchase////Created by Jacky-mac on 15-6-29./ /Copyright (c) www.train.com 2015. All rights reserved.//#import ' YJTg.h ' @implementation yjtg-(instancetype) initwithdict: (Nsdictionary *) dict{self = [su    Per Init];    if (self) {[self setvaluesforkeyswithdictionary:dict]; } return self;} + (Instancetype) tgwithdict: (nsdictionary *) dict{return [[Self alloc]initwithdict:dict];} + (Nsmutablearray *) tgs{nsarray *array = [Nsarray arraywithcontentsoffile:[[nsBundle Mainbundle] pathforresource:@ "tgs.plist" oftype:nil];    Nsmutablearray *arraym = [Nsmutablearray array];    For (nsdictionary *dict in array) {[Arraym addobject:[self tgwithdict:dict]]; } return Arraym;} @endView////yjtgcell.h//01-tableviewcell-buy////Created by Jacky-mac on 15-6-29.//Copyright (c) 2015 www.train.com . All rights reserved.//#import <UIKit/UIKit.h> @class yjtg @interface yjtgcell:uitableviewcell/** buy data Model */@ Property (Nonatomic,strong) YJTG *tg;/** provides a class method to quickly create a Cell */+ (instancetype) Cellwithtableview: (UITableView *) TableView; @end////yjtgcell.m//01-tableviewcell-buy////Created by Jacky-mac on 15-6-29.//Copyright (c) 2015 Www.tra in.com. All rights reserved.//#import "YJTgCell.h" #import "YJTg.h" @interface Yjtgcell () @property (weak, nonatomic) Iboutlet Uiimageview *iconview; @property (weak, nonatomic) Iboutlet UILabel *titlelabel; @property (weak, nonatomic) Iboutlet UILabel *pricelabel; @property (weak, nonatomic) Iboutlet UILabel *buycountlabel; @end @implementation yjtgcell+ (instancetype) Cellwithtableview: (UITableView *) tableView{//1.        Reusable identifier static NSString *id = @ "ID"; 2.        TableView Query Reusable cell Yjtgcell *cell = [TableView dequeuereusablecellwithidentifier:id]; 3. If there is no reusable cell if (cell ==nil) {cell = [[[NSBundle Mainbundle] loadnibnamed:@ "Yjtgcell" Owner:nil Options:nil] Las    TObject]; } return cell;            -(void) SETTG: (YJTG *) tg{//Setter method, the first sentence to be assigned, or to use the model in other methods, will not be able to access to _TG = TG;    Self.titleLabel.text = Tg.title;    Self.iconView.image = [UIImage ImageNamed:tg.icon];    Self.priceLabel.text = Tg.price; Self.buyCountLabel.text = Tg.buycount;} #pragma mark-the method provided by the template/** initialization method is called when the cell is created using code, and if Xib or storyboard is used, this method is not called */-(ID) Initwithstyle: ( Uitableviewcellstyle) style Reuseidentifier: (NSString *) reuseidentifier{self = [Super Initwithstyle:style    Reuseidentifier:reuseidentifier]; if (self) {//initialization code} return self;} /** After being loaded from xib, it will be called automatically, if the use of pure code, will not be executed */-(void) awakefromnib{}/** cell is selected or unchecked is will be called if the custom cell control, All child controls should be added to Contentview */-(void) setselected: (BOOL) selected animated: (BOOL) animated{[Super setselected:selected    Animated:animated]; Configure The view for the selected state}////yjtgfooterview.h//01-tableviewcell-buy////Created by Jacky-mac on 15 -6-29.//Copyright (c) 2015 www.train.com. All rights reserved.//#import <UIKit/UIKit.h> @class yjtgfooterview @protocol yjtgfooterviewdelegate < The download button for the nsobject>/** view is clicked */-(void) Tgfooterviewdiddownloadbuttonclick: (Yjtgfooterview *) footerview;@ End@interface Yjtgfooterview:uiview@property (nonatomic,weak) ID <yjtgfooterviewdelegate>delegate;+ ( instancetype) footerview;/** Refresh data After the end of the update footer view display */-(void) Endrefresh; @end////yjtgfooterview.m//01-tableviewcell-Group purchase/ Created by Jacky-mac on 15-6-29.//Copyright (c) 2015 www.train.com. All rights reserved.//#import "YJTgFooterView.h" @interface Yjtgfooterview () @property (Weak, nonatomic) Iboutlet UIButton *loadmorebutton; @property (weak, nonatomic) Iboutlet UIView *tipsview; @end @implementation yjtgfooterview+ (instancetype) footerview{return [[[NSBundle Mainbundle] loadnibnamed:@ "Yjtgfooterview" Owner:nil Options:nil] lastobject];}        -(Ibaction) loadmore{//nslog (@ "load more"); 1.            Hide button Self.loadMoreButton.hidden = YES; 2.        Show Prompt View Self.tipsView.hidden = NO; Delay execution command, multithreading GCD//Future operation on delay, unified use dispatch_after dispatch_time_t poptime = Dispatch_time (Dispatch_time_now, (int64_t)    (1.0 * nsec_per_sec)); The code in Dispatch_after (Poptime, Dispatch_get_main_queue (), ^ (void) {//block code is executed after 1 seconds//3. Load data (loaded from a network server        , it takes time ...)        View is used to display the data, with the agent to achieve! Agent is used to listen, when something happens, notify the "agent"-"controller to" work "load data//3.1 Determine whether the agent implements the Protocol method if ([Self.delegate respondstose Lector: @selector (Tgfooterviewdiddownloadbuttonclick:)]) {[Self.delegate Tgfooterviewdiddownloadbutt OncliCk:self]; }//4.        Loading complete data Self.loadMoreButton.hidden = NO;            Self.tipsView.hidden = YES; });}    -(void) endrefresh{//4. Loading complete data Self.loadMoreButton.hidden = NO; Self.tipsView.hidden = YES;} yjtgheadview.h//01-tableviewcell-buy////Created by Jacky-mac on 15-6-30.//Copyright (c) 2015 www.train.com.  All rights reserved.//#import <UIKit/UIKit.h> @interface yjtgheadview:uiview+ (instancetype) Hesdview; @end//// yjtgheadview.m//01-tableviewcell-buy////Created by Jacky-mac on 15-6-30.//Copyright (c) 2015 www.train.com. All rights reserved.//#import "YJTgHeadView.h" #define Kimagecount 5@interface Yjtgheadview () <uiscrollviewdelegate > @property (Weak, nonatomic) Iboutlet Uiscrollview *scrollview; @property (weak, nonatomic) Iboutlet Uipagecontrol * Pagecontrol; @property (Nonatomic,strong) Nstimer *timer; @end @implementation yjtgheadview+ (instancetype) HesdView; {return [[[NSBundle Mainbundle] loadnibnamed:@ Yjtgheadview"Owner:nil Options:nil] lastobject];}    -(void) awakefromnib{//1. Add picture cgfloat Imagew = self.scrollView.frame.size.width;    CGFloat Imageh = self.scrollView.frame.size.height;    CGFloat imagey = 0;                for (int index = 0; index < kimagecount; index++) {cgfloat ImageX = index * IMAGEW;        Uiimageview *imageview = [[Uiimageview alloc] init];        Imageview.frame = CGRectMake (ImageX, Imagey, Imagew, Imageh);                Imageview.image = [UIImage imagenamed:[nsstring stringwithformat:@ "ad_%02d", index]];    [Self.scrollview Addsubview:imageview];        }//2. Set Content Size self.scrollView.contentSize = Cgsizemake (Imagew * kimagecount, 0);      Remove horizontal ScrollBar self.scrollView.showsVerticalScrollIndicator = NO;        Self.scrollView.showsHorizontalScrollIndicator = NO;        Cancel the Spring effect self.scrollView.bounces = NO;    Set proxy self.scrollView.delegate = self;            Whether paging self.scrollView.pagingEnabled = YES; 3. Paging SELF.PAGECONTROL.NUmberofpages = Kimagecount; 4. Timer Self.timer = [Nstimer timerwithtimeinterval:2.0 target:self selector: @selector (nextimage) Userinfo:nil repeats    : YES];  [[Nsrunloop Mainrunloop] AddTimer:self.timer formode:nsrunloopcommonmodes]; }-(void) nextimage{//1. Next if (Self.pageControl.currentPage = = kImageCount-1) {Self.pageControl.curr    entpage = 0;    }else{self.pagecontrol.currentpage++;    }//2. Set scrolling Cgpoint offset = cgpointmake (self.scrollView.frame.size.width * self.pageControl.currentPage, 0); [Self.scrollview Setcontentoffset:offset animated:yes];}    #pragma mark-Proxy Method-(void) scrollviewdidenddragging: (Uiscrollview *) ScrollView willdecelerate: (BOOL) decelerate{    Self.timer = [Nstimer timerwithtimeinterval:2.0 target:self selector: @selector (nextimage) Userinfo:nil Repeats:yes];    [[Nsrunloop Mainrunloop] AddTimer:self.timer formode:nsrunloopcommonmodes];    }-(void) scrollviewwillbegindecelerating: (Uiscrollview *) scrollview{[Self.timer invalidate]; Self.timer = nil;}    -(void) Scrollviewdidscroll: (Uiscrollview *) scrollview{if (Self.timer) return; Self.pageControl.currentPage = (scrollview.contentoffset.x + scrollView.frame.size.width * 0.5)/ ScrollView.frame.size.width;}  @endController////yjviewcontroller.m//01-tableviewcell-buy////Created by Jacky-mac on 15-6-29.//Copyright (c) 2015 Www.train.com. All rights reserved.//#import "YJViewController.h" #import "YJTg.h" #import "YJTgCell.h" #import " YJTgFooterView.h "#import" YJTgHeadView.h "@interface Yjviewcontroller () <YJTgFooterViewDelegate> @property ( Nonatomic,strong) Nsmutablearray *tgs, @end @implementation yjviewcontroller-(Nsarray *) tgs{if (_tgs = nil) {_t    GS = [YJTG TGS]; } return _TGS;}        -(void) viewdidload{[Super Viewdidload];        Self.tableView.rowHeight = 80;    Adjust the margin so that the table view can be left out of the status bar;          Self.tableView.contentInset = Uiedgeinsetsmake (20, 0, 0, 0); Load last view from Xib set to Footerview//Self.tableView.tabLefooterview = [[[NSBundle Mainbundle] loadnibnamed:@ "Yjtgfooterview" Owner:nil Options:nil] lastObject];    Yjtgfooterview *footer = [Yjtgfooterview Footerview];    Footer.delegate = self;        Self.tableView.tableFooterView = footer; Self.tableView.tableHeaderView = [Yjtgheadview Hesdview];} Proxy method for #pragma mark-footview-(void) Tgfooterviewdiddownloadbuttonclick: (Yjtgfooterview *) footerview{//Load Data NSLog (@      "Trying to load data ....");    Double delayinseconds = 2.0;    dispatch_time_t poptime = Dispatch_time (Dispatch_time_now, (int64_t) (Delayinseconds * nsec_per_sec)); Dispatch_after (Poptime, Dispatch_get_main_queue (), ^ (void) {/////////Add data to array, simulate Network load completion, after getting network data        The effect of nsdictionary *dict = @{@ "title": @ "haha", @ "icon": @ "ad_00", @ "price": @ "100.2" @ "Buycount": @ "250"};                YJTG *TG = [YJTG tgwithdict:dict];                        [Self.tgs ADDOBJECT:TG];        Refresh data//[Self.tableview reloaddata]; Create a new Indexpath NsinDexpath *path = [Nsindexpath indexpathforrow: (self.tgs.count-1) insection:0];            [Self.tableview Insertrowsatindexpaths:@[path] withrowanimation:uitableviewrowanimationmiddle];    }); } #pragma mark-Data source Method-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{return Self.tgs.count;} -(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{//1. Create CE    ll Yjtgcell *cell = [Yjtgcell Cellwithtableview:tableview];        2> through the data model, the cell content can be set so that the view controller does not need to know the implementation details inside the cell CELL.TG = Self.tgs[indexpath.row]; return cell;} @end

iOS Learning-Tableviewcell (group purchase)

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.