27th article: QQ friends list, Uitableviewheaderfooterview usage

Source: Internet
Author: User
Tags set background

1. Set the TableView Headerview Note to the following:
#pragma Mark-tableview Proxy Method-(UIView *) TableView: (UITableView *) TableView viewforheaderinsection: (Nsinteger) section{    //Get a custom Headerview    Qjheaderview * Headerview = [Qjheaderview Headerviewwithtableview:tableview];        Set data    Headerview.friendgroup = self.friendgroups[section];        Set proxy object    headerview.delegate = self;        return Headerview;} -(CGFloat) TableView: (UITableView *) TableView heightforheaderinsection: (nsinteger) section{    return 60;}
Uitableviewheaderfooterview:
/** when the initialization Headerview is finished, the system automatically calls the method to set the child control's frame, which is the method in UIView */
-(void) layoutsubviews;
"Can be reused, similar to UITableViewCell
UIButton:
*//* The following two settings make the picture conversion angle is not deformed
Do I need to cut out the part of the country when the picture goes beyond the bounds border?
HeaderBtn.imageView.clipsToBounds = NO;
Set Picture content mode centered
HeaderBtn.imageView.contentMode = Uiviewcontentmodecenter;
//Add HEADERBTN to monitor click events
[Headerbtn addtarget:self Action: @selector (headerviewdidclicked) forcontrolevents:uicontroleventtouchupinside];
Set HEADERBTN Content Horizontal alignment

Headerbtn.contenthorizontalalignment = Uicontrolcontenthorizontalalignmentleft;




Code MVC Pattern:

Model:

////qjfriend.h//11-qq buddy list////Created by Gujer on 15/10/3.//copyright©2015 year itcast. All rights reserved.//#import <Foundation/Foundation.h> @interface qjfriend:nsobject@property (nonatomic,copy) NSString * icon; @property (nonatomic,copy) NSString * intro; @property (nonatomic,copy) nsstring * name; @property ( NONATOMIC,ASSIGN,GETTER=ISVIP) BOOL vip;+ (instancetype) friendwithdictionary: (Nsdictionary *) dic;-(instancetype) Initwithdictionary: (Nsdictionary *) dic; @end 
  qjfriend.m//  11-qq friends list////  Created by Gujer on 15/10/3.//  copyright©2015 year itcast. All rights reserved.//#import "QJFriend.h" @implementation qjfriend+ (Instancetype) Friendwithdictionary: ( Nsdictionary *) dic{    return [[Self alloc] initwithdictionary:dic];} -(Instancetype) Initwithdictionary: (nsdictionary *) dic{        if (self = [super init]) {        Self.icon = dic[@ "icon"];        Self.name = dic[@ "name"];        Self.intro = dic[@ "Intro"];        SELF.VIP = [dic[@ "VIP"] intvalue];    }    return self;} @end

////qjfriendgroup.h//11-qq buddy list////Created by Gujer on 15/10/3.//copyright©2015 year Itcas T. All rights reserved.//#import <Foundation/Foundation.h> @class qjfriend @interface qjfriendgroup:nsobject/** Storage is the qjfrined model */@property (Nonatomic,strong) nsarray * friends; @property (nonatomic,copy) nsstring * name; @property ( nonatomic,assign) int online; @property (nonatomic,assign,getter=isgroupopen) BOOL groupopened;+ (instancetype) Friendgroupwithdictionary: (Nsdictionary *) dic;-(instancetype) initwithdictionary: (Nsdictionary *) dic; @end 
  qjfriendgroup.m//  11-qq friends list////  Created by Gujer on 15/10/3.//  copyright©2015 year itcast. All rights reserved.//#import "QJFriendGroup.h" #import "QJFriend.h" @implementation qjfriendgroup+ (Instancetype) Friendgroupwithdictionary: (nsdictionary *) dic{    return  [[Qjfriendgroup alloc]initwithdictionary:dic];} -(Instancetype) Initwithdictionary: (nsdictionary *) dic{    if (self = [super init]) {        self.name = dic[@ "name"];        Self.online = [dic[@ "online"] intvalue];        Self.friends = dic[@ "Friends"];        self.groupopened = NO;        Nsmutablearray * Friends = [Nsmutablearray array];        For (Nsdictionary * dict in self.friends) {            qjfriend * frienddata = [qjfriend friendwithdictionary:dict];            [Friends Addobject:frienddata];        }        Self.friends = friends;    }        return self;} @end

View:

  qjfriendcell.h//  11-qq friends list////  Created by Gujer on 15/10/3.//  copyright©2015 year itcast. All rights reserved.//#import <UIKit/UIKit.h> @class qjfriend @interface qjfriendcell:uitableviewcell@property (Nonatomic, weak) Qjfriend * frienddata;+ (instancetype) Friendcellwithtableview: (UITableView *) TableView; @end

  qjfriendcell.m//  11-qq friends list////  Created by Gujer on 15/10/3.//  copyright©2015 year itcast. All rights reserved.//#import "QJFriendCell.h" #import "QJFriend.h" @implementation qjfriendcell+ (Instancetype) Friendcellwithtableview: (UITableView *) tableview{    static NSString * ID = @ "Friendcell";    Qjfriendcell * cell = [TableView dequeuereusablecellwithidentifier:id];    if (cell = = nil) {        cell = [[self alloc]initwithstyle:uitableviewcellstylesubtitle reuseidentifier:id];    }    return cell;} -(void) Setfrienddata: (Qjfriend *) frienddata{    _frienddata = frienddata;        Self.imageView.image = [UIImage ImageNamed:friendData.icon];    Self.textLabel.text = Frienddata.name;    Self.detailTextLabel.text = Frienddata.intro;} @end
Main:
  qjheaderview.h//  11-qq friends list////  Created by Gujer on 15/10/3.//  copyright©2015 year itcast. All rights reserved.//#import <UIKit/UIKit.h> @class qjfriendgroup, Qjheaderview; @protocol qjheaderviewdelegate <NSObject> @optional-(void) headerviewdidclicked: (Qjheaderview *) Headerview; @end @interface Qjheaderview: Uitableviewheaderfooterview@property (nonatomic, weak) qjfriendgroup * friendgroup; @property (nonatomic, weak) id< Qjheaderviewdelegate> delegate;+ (Instancetype) Headerviewwithtableview: (UITableView *) TableViw; @end

qjheaderview.m//11-qq Friends List////Created by Gujer on 15/10/3.//copyright©2015 year itcast. All rights reserved.//#import "QJHeaderView.h" #import "QJFriendGroup.h" @interface Qjheaderview () @property (nonatomic , weak) UIButton * HEADERBTN; @property (nonatomic, weak) UILabel * headerlabel;-(void) headerviewdidclicked;@ End@implementation qjheaderview/** Create a Headerview */+ (instancetype) Headerviewwithtableview: (UITableView *) TABLEVIW    {static NSString * ID = @ "Headerview";    Find Reusable Headerview Qjheaderview * Headerview = [Tableviw dequeuereusableheaderfooterviewwithidentifier:id];    if (Headerview = = nil) {Headerview = [[Qjheaderview alloc]initwithreuseidentifier:id];    }//headerView.friendGroup.groupOpened = NO; return Headerview;} /** Override initialization method Add control */-(instancetype) Initwithreuseidentifier: (NSString *) reuseidentifier{if (self = [Super Initwithr       Euseidentifier:reuseidentifier]) {//1. Add a by-chick UIButton * headerbtn = [[UIButton alloc]init]; Set padding headerbtn.titleedgeinsets = Uiedgeinsetsmake (0, 10, 0, 0);        Headerbtn.contentedgeinsets = Uiedgeinsetsmake (0, 10, 0, 0);        Set background Image [Headerbtn setbackgroundimage:[uiimage imagenamed:@ "BUDDY_HEADER_BG"] forstate:uicontrolstatenormal]; [Headerbtn setbackgroundimage:[uiimage imagenamed:@ "buddy_header_bg_highlighted"] forState:        Uicontrolstatehighlighted]; You must call the method when you set the image in the button, and you cannot use it.  Imageview.image to set [headerbtn setimage:[uiimage imagenamed:@ "Buddy_header_arrow"] forstate: (UIControlStateNormal)]                ;        * The following two settings make the picture conversion angle is not deformed//when the country picture beyond the boundaries of the bounds if you need to cut out the part of headerBtn.imageView.clipsToBounds = no;                Set picture content mode centered headerBtn.imageView.contentMode = Uiviewcontentmodecenter;        Set the font color for title [Headerbtn Settitlecolor:[uicolor Blackcolor] forstate:uicontrolstatenormal]; Add HEADERBTN Listener Click event [headerbtn addtarget:self Action: @selector (headerviewdidclicked) ForcontroLevents:uicontroleventtouchupinside];        Set HEADERBTN content Horizontal Alignment headerbtn.contenthorizontalalignment = Uicontrolcontenthorizontalalignmentleft;        [Self.contentview ADDSUBVIEW:HEADERBTN];                        SELF.HEADERBTN = headerbtn;        2. Add a Label control UILabel * Headerlabel = [[UILabel alloc]init];        Headerlabel.textalignment = Nstextalignmentright;        Headerlabel.textcolor = [Uicolor Graycolor];        [Self.contentview Addsubview:headerlabel];            Self.headerlabel = Headerlabel; } return self;} /** call */-(void) headerviewdidclicked{//state Transform self.friendGroup.groupOpened =!SELF.FRIENDGROUP.G when headerbtn by Chick is clicked    roupopened; The picture turns an angle if (self.friendGroup.isGroupOpen) {self.headerBtn.imageView.transform = Cgaffinetransformmakerotation    (m_pi_2);    } else{self.headerBtn.imageView.transform = cgaffinetransformmakerotation (0); }//Call proxy method if ([Self.delegate respondstoselector: @selector (H(eaderviewdidclicked:)]) {[Self.delegate headerviewdidclicked:self]; }}/** when the initialization Headerview is complete, the system automatically calls the method to set the child control's frame */-(void) layoutsubviews{//NSLog (@ "%@---%@", nsstringfromcgrect (    Self.bounds), Nsstringfromcgrect (Self.frame));        #warning mark-Make sure to call, otherwise the child controls have some functionality that cannot be used//NOTE: Be sure to initialize the parent [Super Layoutsubviews];        Sets the position of the child control in the Contentview self.headerBtn.frame = self.bounds;    CGFloat W = 100;    CGFloat H = self.frame.size.height;    CGFloat Y = 0;    CGFloat X = self.frame.size.width-w-10; Self.headerLabel.frame = CGRectMake (X, Y, W, H);}    /** Initialize content in Headerview */-(void) Setfriendgroup: (Qjfriendgroup *) friendgroup{_friendgroup = Friendgroup;        Set content in Headerbtn [self.headerbtn setTitle:friendGroup.name forstate:uicontrolstatenormal]; Set content in Headerlabel Self.headerLabel.text = [NSString stringwithformat:@ "%d/%lu", Friendgroup.online, FriendGroup.friends.count];} @end

Controller:

  viewcontroller.h//  11-qq friends list////  Created by Gujer on 15/10/3.//  copyright©2015 year itcast. All rights reserved.//#import <UIKit/UIKit.h> @interface viewcontroller:uiviewcontroller@end
viewcontroller.m//11-qq Friends List////Created by Gujer on 15/10/3.//copyright©2015 year itcast. All rights reserved.//#import "ViewController.h" #import "QJFriendGroup.h" #import "QJFriendCell.h" #import " QJHeaderView.h "@interface Viewcontroller () <uitableviewdatasource,uitableviewdelegate,qjheaderviewdelegate > @property (Weak, nonatomic) Iboutlet UITableView *tableview; @property (strong,nonatomic) Nsarray * friendgroups;@    End@implementation viewcontroller-(void) viewdidload {[Super viewdidload];    Self.tableView.dataSource = self;    Self.tableView.delegate = self;    Set the height of each cell per group self.tableView.rowHeight = 60; }-(BOOL) prefersstatusbarhidden{return YES; /** Initialize data content */-(Nsarray *) friendgroups{if (_friendgroups = nil) {Nsarray * groups = [Nsarray arraywithconten        Tsoffile:[[nsbundle mainbundle]pathforresource:@ "Friends.plist" Oftype:nil]];        Nsmutablearray * friendgroups = [Nsmutablearray array];          For (Nsdictionary * dic in groups) {  Qjfriendgroup * Friendgroup = [Qjfriendgroup friendgroupwithdictionary:dic];        [Friendgroups Addobject:friendgroup];    } _friendgroups = friendgroups; } return _friendgroups;} #pragma mark-tableview Data source Method-(Nsinteger) Numberofsectionsintableview: (UITableView *) tableview{return Self.friendGroups.count;} -(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{Qjfriendgroup * group = Self    . Friendgroups[section]; Return (Group.isgroupopen group.friends.count:0);}     -(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{//Create a cell        Qjfriendcell * cell = [Qjfriendcell Friendcellwithtableview:tableview];    Set Data Qjfriendgroup * group = self.friendgroups[indexpath.section];    Qjfriend * frienddata = Group.friends[indexpath.row];        Cell.frienddata = Frienddata; return cell;} -(NSString *) TableView: (UITableView *) TableView TitleforheaderinsectIon: (Nsinteger) section{//qjfriendgroup * Group = self.friendgroups[section];//return group.name;//} #pragma mark- TableView Proxy Method-(UIView *) TableView: (UITableView *) TableView viewforheaderinsection: (nsinteger) section{//Get a custom        Headerview Qjheaderview * Headerview = [Qjheaderview Headerviewwithtableview:tableview];        Set data Headerview.friendgroup = Self.friendgroups[section];        Set proxy object headerview.delegate = self; return Headerview;} -(CGFloat) TableView: (UITableView *) TableView heightforheaderinsection: (nsinteger) section{return 60;} #pragma Mark-qjheaderview proxy Method-(void) headerviewdidclicked: (Qjheaderview *) headerview{[Self.tableview reloaddata];} @end


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

27th article: QQ friends list, Uitableviewheaderfooterview usage

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.