Ios-uitableviewcell Expand

Source: Internet
Author: User

#import <UIKit/UIKit.h>

@interface Zsdhelpcell:uitableviewcell

@property (Weak, nonatomic) Iboutlet Uiimageview *selectimageview;

@property (nonatomic,copy) NSString *question;

@property (nonatomic,copy) NSString *answer;

Gets the height after expansion

-(cgfloat) getexpandheight;

@end

#import "ZSDHelpCell.h"

Judging the system version

#define IOS_VERSION_7_OR_ABOVE ([[[[Uidevice Currentdevice] systemversion] floatvalue] >= 8.0)? (YES):(NO))

@interface Zsdhelpcell ()

@property (Weak, nonatomic) Iboutlet UILabel *questionlabel;

@property (Weak, nonatomic) Iboutlet UILabel *answerlabel;

@end

@implementation Zsdhelpcell

-(void) awakefromnib

{

Initialization code

}

-(void) setselected: (BOOL) selected animated: (BOOL) animated

{

[Super setselected:selected animated:animated];

Configure The view for the selected state

}

-(void) Setanswer: (NSString *) answer

{

if (_answer!=answer)

{

_answer=answer;

_answerlabel.text=_answer;

}

}

-(void) Setquestion: (NSString *) question

{

if (_question!=question)

{

_question=question;

_questionlabel.text=_question;

}

}

-(CGFloat) getexpandheight

{

iOS8 is effective for systemlayoutsizefittingsize this method

Return ios_version_7_or_above==1? [Self systemlayoutsizefittingsize:uilayoutfittingcompressedsize].height:120;

}

@end

#import <UIKit/UIKit.h>

@interface viewcontroller:uiviewcontroller<uitableviewdatasource,uitableviewdelegate>

@property (Weak, nonatomic) Iboutlet UITableView *mytableview;

@end

#import "ViewController.h"

#import "ZSDHelpCell.h"

#define Kdefaultheight 44.0f

#define Ktabeleheaderheight 40.0f

Background color

#define Kthemebackgroundcolor [Uicolor colorwithred:0.93 green:0.92 blue:0.92 alpha:1]

#pragma mark-life Circle

@interface Viewcontroller ()

{

Nsmutablearray *expandarray;//Expanded Array

Nsmutabledictionary *cellheightdic;//Set the cell height dictionary

Nsmutabledictionary *datasourcedic;//Read plist file contents

}

@end

@implementation Viewcontroller

-(void) viewdidload

{

[Super Viewdidload];

_mytableview.backgroundcolor=kthemebackgroundcolor;

[Self initializationarrayordictionary];

[Self loadcontentfromplist];

Additional setup after loading the view, typically from a nib.

}

-(void) didreceivememorywarning

{

[Super didreceivememorywarning];

Dispose of any resources the can be recreated.

}

#pragma mark-private

-(void) initializationarrayordictionary

{

Expandarray=[nsmutablearray array];

Cellheightdic=[nsmutabledictionary dictionary];

Datasourcedic=[nsmutabledictionary dictionary];

}

-(void) loadcontentfromplist

{

NSString *plistpath=[[nsbundle Mainbundle] pathforresource:@ "Content" oftype:@ "plist"];

Datasourcedic=[nsmutabledictionary Dictionarywithcontentsoffile:plistpath];

NSLog (@ "datasourcedic=%@", [Datasourcedic allvalues]);

}

#pragma mark-uitableviewdatasource

-(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView

{

return datasourcedic.count;

}

-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section

{

Nsarray *sectionarray=[datasourcedic allvalues];

Nsdictionary *rowdic=[sectionarray Objectatindex:section];

return rowdic.count;

}

-(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) Indexpath

{

if ([Expandarray Containsobject:indexpath])

{

NSString *key=[nsstring stringwithformat:@ "%ld", indexpath.section];

if ([Cellheightdic Objectforkey:key])

{

return [[Cellheightdic Objectforkey:key] floatvalue];

}

}

return kdefaultheight;

}

-(CGFloat) TableView: (UITableView *) TableView heightforheaderinsection: (nsinteger) Section

{

return ktabeleheaderheight;

}

-(UIView *) TableView: (UITableView *) TableView viewforheaderinsection: (nsinteger) Section

{

Section add a View and label

CGRect temprect=cgrectmake (0, 0, tableview.bounds.size.width,ktabeleheaderheight);

UIView *headerview = [[UIView alloc]initwithframe:temprect];

Headerview.backgroundcolor=kthemebackgroundcolor;

Nsarray *sectionarray=[datasourcedic AllKeys];

NSString *text=[sectionarray Objectatindex:section];

UILabel *textlabel = [[UILabel alloc]initwithframe:cgrectmake (12.0f, 14.0f, 100.0f, 15.0f)];

Textlabel.font = [Uifont systemfontofsize:15.0f];

Textlabel.backgroundcolor = [Uicolor Clearcolor];

Textlabel.textcolor = [Uicolor Graycolor];

Textlabel.text = text;

[Headerview Addsubview:textlabel];

return headerview;

}

-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath

{

Zsdhelpcell *cell= (Zsdhelpcell *) [TableView dequeuereusablecellwithidentifier:@ "cell" forindexpath:indexpath];

Nsarray *sectionarray=[datasourcedic allvalues];

Nsdictionary *rowdic=[sectionarray ObjectAtIndex:indexPath.section];

Nsarray *questionlist=[rowdic AllKeys];

Nsarray *answerlist=[rowdic allvalues];

Cell.question=[questionlist ObjectAtIndex:indexPath.row];

Set the height of each cell key

NSString *key=[nsstring stringwithformat:@ "%ld", indexpath.section];

if (![ Cellheightdic Objectforkey:key])

{

Cell.answer=[answerlist ObjectAtIndex:indexPath.row];

CGFloat Height=[cell Getexpandheight];

[Cellheightdic Setobject:[nsnumber numberwithfloat:height] forkey:key];

}

Hide cell

UIImage *normalimg = [UIImage imagenamed:@ "Member_icon_more"];

Expand cell

UIImage *selectimg = [UIImage imagenamed:@ "Common_icon_down"];

if ([Expandarray Containsobject:indexpath])

{

cell.selectimageview.image=selectimg;

Cell.answer=[answerlist ObjectAtIndex:indexPath.row];

}

Else

{

cell.selectimageview.image=normalimg;

Cell.answer=nil;

}

return cell;

}

#pragma mark-uitableviewdelegate

-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) Indexpath

{

If the index does not exist in the array, add it to the array

if (![ Expandarray Containsobject:indexpath])

{

[Expandarray Addobject:indexpath];

}

Otherwise, it is removed from the array

Else

{

[Expandarray Removeobject:indexpath];

}

[TableView Reloadrowsatindexpaths:@[indexpath] withrowanimation:uitableviewrowanimationautomatic];

}

@end

Ios-uitableviewcell Expand

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.