#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 expanded height

-(CGFloat)getExpandHeight;

@end

#import "ZSDHelpCell.h"

// Judge 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 about systemLayoutSizeFittingSize This method works

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 up cell High Dictionary

NSMutableDictionary *dataSourceDic;// Read plist The contents of the document

}

@end

@implementation ViewController

- (void)viewDidLoad

{

[super viewDidLoad];

_myTableView.backgroundColor=kThemeBackGroundColor;

[self InitializationArrayOrDictionary];

[self loadContentFromPlist];

// Do any additional setup after loading the view, typically from a nib.

}

- (void)didReceiveMemoryWarning

{

[super didReceiveMemoryWarning];

// Dispose of any resources that 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 one 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 each cell Height 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"];

// an 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 , So add it to the array

if(![expandArray containsObject:indexPath])

{

[expandArray addObject:indexPath];

}

// Otherwise, remove... From the array

else

{

[expandArray removeObject:indexPath];

}

[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

}

@end

ios-uitableviewcell More related articles

  1. iOS UITableViewCell UITableVIewController Pure code development

    iOS UITableViewCell UITableVIewController Pure code development < original > . Pure code Customize UITableViewCell Go straight to the code ////// #imp ...

  2. iOS UITableViewCell Of &quot; Sliding multiple buttons &quot;

    This article is authorized to reprint , author :@ Summer is an adult Preface : The purpose of this blog is to introduce tableviewcell Some of the sliding " things ", I was shopping yesterday github When I saw it, it was a very interesting tripartite Library , It's easy to use it for a while ...

  3. iOS UITableViewCell When clicking, the sub view background is transparent

    Doing it iOS Project development ,UITableView Control is widely used . In the process of customization UITableViewCell when , I often have problems like this : stay UITableViewCell A sub view with a background color is added to it , When ...

  4. ios UITableViewCell Reuse problem

    Writing sina Microblog interface is used in the process of cell, So it's in cell Add some controls on , But because the content of each microblog is different , So in the process of displaying , There's a problem of overlapping content , In fact, that is UITableViewCell reusing ...

  5. iOS UITableViewCell Slide delete

    In general, we use the form of a list to display data UITableView. After mastering the use of UITableView After showing the data , During the development process, you may encounter the need to delete data , We want to achieve a stroke on a line of data , And then there's a delete ...

  6. iOS UITableViewCell To the left 15(cell The length is full width )

    iOS7 Under the circumstances : tableView.separatorInset = UIEdgeInsetsZero; iOS8.9 Under the circumstances : First, in the viewDidLoad Method with the following code : if ([table ...

  7. IOS - UITableViewCell The color and tableViewCell Of selecte And deselecte

    1. System default color settings [cpp] view plaincopy // colourless cell.selectionStyle = UITableViewCellSelectionStyleNone; // Blue ...

  8. iOS UITableViewCell in adjustment imageView Picture size for

    In my project , Many places hope that UITableViewCell Medium imageView Can display according to the size of their own pictures , In order to solve this problem, I feel that UITableViewCell It's not worth it . as follows : ...

  9. ios uitableviewcell Dynamic computing height

    #import <UIKit/UIKit.h> @interface TestCell : UITableViewCell @property (weak, nonatomic) IBOu ...

  10. Click on UITableviewCell Expand and contract

    #import "ViewController.h" #import "ZSDTestCell.h" @interface ViewController ()& ...

Random recommendation

  1. Talking about JAVA Design patterns

    No million lines of code , Don't try to figure out the design patterns . At present, my code is about 6500 lines ,2016 We need to continue to work hard in this year , Finish the appointment of Wanxing as soon as possible . After work, I need , Let's discuss design patterns in advance . Create pattern , Five kinds in total : Factory mode . Abstract work ...

  2. EF Core 1.0 and SQLServer 2008 Paging problem

    EF Core 1.0 stay sqlserver2008 When paging, you need to specify the number of paging . EF Core1.0 The generated paging statement uses Featch Next. This statement can only be used in SqlServer2012 When ...

  3. centos View set port open status

    centos Check to see if the port is open /etc/init.d/iptables status centos Open ports /sbin/iptables -I INPUT -p tcp --dport 8000 -j ...

  4. maven Install... That does not exist in the repository jar package

    Here we use ojdbc6.jar As a case study First of all, mine ojdbc6.jar Put it in D The root directory of the disk D:\ojdbc6.jar And then we open up cmd Command window , Run the command :mvn install:install-file -D ...

  5. solve div Inside img It's a gap problem ~

    solve div Inside img It's a gap problem picture IMG What if there is a gap between the container and the lower boundary ? Here are 3 The simple solution to this problem . First of all , Give the picture img label display:block. img{display:block} second , set ...

  6. 【 turn 】sublime text 2 Chinese garbled solution

    sublime text 2 Is a very good cross platform text and source code editor , I like it very much , But not supported GB2312 and GBK Coding can be cumbersome at times . Can be passed to sublime text Add the code type conversion package ( such as ...

  7. MySQL Optimization must be adjusted 10 Item Configuration

    When we're hired to monitor MySQL Performance time , People want us to look at MySQL Configuration and then give some suggestions for improvement . Many people are very surprised after the event , Because we suggest they just change a few settings , Even if there are hundreds of configuration items . The purpose of this article is ...

  8. Java Realize stack through linked list

    class LinkedStack<T> { private Node top; private int size; /** * Initialization stack */ public LinkedStack() ...

  9. Use promisify To process asynchronous operations

    modern js Include nodejs Many of the functions in are executed asynchronously , We always need to write a callback function and pass it in as a last parameter , And what I want is to write like promise Such a callback promise .then() .then() ...

  10. jvm(1) Class loading ( 3、 ... and )( Thread context loader )

    brief introduction : Class loader from JDK 1.0 And that's what happened , At first it was to satisfy Java Applet Developed to meet the needs of . Java Applet You need to download it remotely Java Class file to the browser and execute . Now the class loader is ...