標籤:des style http io color ar os 使用 sp
1.自訂cell
2.多種cell 的混合使用
3.cell自適應高度
自訂cell就是建立一個UITableViewCell的子類。
把cell上的控制項建立都封裝在子類中,簡化UIViewController中的代碼
子視圖控制項添加到cell的contentView上
cell中的控制項如何顯示Model中的資訊?
cell中聲明一個Model類型的屬性,viewController中擷取到Model對象後賦值給cell的Model屬性,cell中重寫Model的setter方法,把Model對象中的內容重新賦值給各個控制項M和V不直接進行通訊,C負責M和V之間進行通訊
通常我們會在tableView:cellForRowAtIndexPath:方法中根據不同的
Model來決定使用什麼類型的cell每種類型的cell要定義不同的重用標識符cell重用的時候會根據重用標識從重用隊列中取用哪種類型的cell
代碼如下:
建一個Model
#import <Foundation/Foundation.h>@interface Student : NSObject@property(nonatomic,strong)NSString *name;@property(nonatomic,strong)NSString *age;@property(nonatomic,strong)NSString *tel;@property(nonatomic,strong)NSString *qq;@property(nonatomic,strong)NSString *description;@property(nonatomic,strong)NSString *sex;@property(nonatomic,strong)NSString *icon;@end
#import "Student.h"@implementation Student//如果沒有找到相應的key值走這個方法-(void)setValue:(id)value forUndefinedKey:(NSString *)key{}@end
//建立一個檔案plist,用來存放資料
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plist version="1.0"><array><dict><key>name</key><string>小明</string><key>age</key><string>18</string><key>tel</key><string>18888888</string><key>qq</key><string>700000</string><key>sex</key><string>女</string><key>description</key><string>裝逼只是瞬間,不要臉那才是永恒</string><key>icon</key><string>lishijie.png</string></dict><dict><key>name</key><string>小馨</string><key>age</key><string>18</string><key>tel</key><string>110</string><key>qq</key><string>123456</string><key>sex</key><string>女</string><key>description</key><string>裝逼只是瞬間,不要臉那才是永恒</string><key>icon</key><string>Leixin.png</string></dict></array></plist>
建立一個RootTableViewController
#import "RootTableViewController.h"#import "RootTableViewCell.h"#import "Student.h"#import "GirlTableViewCell.h"@interface RootTableViewController ()//用來儲存有多少個數組的@property(nonatomic,strong)NSMutableArray *groupArray;@end@implementation RootTableViewController- (id)initWithStyle:(UITableViewStyle)style{ self = [super initWithStyle:style]; if (self) { // Custom initialization } return self;}- (void)viewDidLoad{ [super viewDidLoad]; //路徑取出資料 NSString *path = [[NSBundle mainBundle] pathForResource:@"student" ofType:@"plist"]; //臨時獲得資料 NSArray *tempArray = [NSArray arrayWithContentsOfFile:path]; //臨時分組存放 NSMutableArray *arr = [NSMutableArray array]; for (NSDictionary *dict in tempArray) { Student *stu = [[Student alloc] init]; //匹配stu中的key [stu setValuesForKeysWithDictionary:dict]; [arr addObject:stu]; } //把臨時分組加入總分組中 self.groupArray = [NSMutableArray arrayWithObject:arr]; }- (void)didReceiveMemoryWarning{ [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}#pragma mark - Table view data source- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ // Return the number of sections. return self.groupArray.count;}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ // Return the number of rows in the section. return [self.groupArray[section] count];}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ Student *stu = self.groupArray[indexPath.section][indexPath.row]; if ([stu.sex isEqualToString:@"男"]) { NSString *cell_id [email protected]"cell_boy"; RootTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cell_id]; if (nil == cell) { cell = [[RootTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cell_id]; } // cell.textLabel.text = @"測試資料"; cell.nameLabel.text = [self.groupArray[indexPath.section][indexPath.row] name]; cell.telLabel.text = [self.groupArray[indexPath.section][indexPath.row] tel]; cell.descriptionLabel.text = [self.groupArray[indexPath.section][indexPath.row] description]; //定義descriptionLabel的fram //[RootTableViewCell textHeight:stu.description]調用方法計算文字的高度 cell.descriptionLabel.frame = CGRectMake(cell.descriptionLabel.frame.origin.x, cell.descriptionLabel.frame.origin.y, cell.descriptionLabel.frame.size.width, [RootTableViewCell textHeight:stu.description]); //計算文字高度方法二:// //擷取label 的高度// CGRect tempRect = cell.descriptionLabel.frame;// //temp的高度等於輸入文字的高度// tempRect.size.height = [RootTableViewCell textHeight:stu.description];// //把temp 的fram給label// cell.descriptionLabel.frame = tempRect;// // cell.descriptionLabel.numberOfLines = 0; return cell; }else{ NSString *cell_id = @"cell_girl"; GirlTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cell_id]; if (nil == cell) { cell = [[GirlTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cell_id]; } cell.nameLabel.text = [self.groupArray[indexPath.section][indexPath.row] name]; cell.telLabel.text = [self.groupArray[indexPath.section][indexPath.row] tel]; cell.iconImage.image = [UIImage imageNamed:stu.icon]; return cell; } }-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ Student *stu = self.groupArray[indexPath.section][indexPath.row]; if ([stu.sex isEqualToString:@"男"]) { return [RootTableViewCell cellHeight:stu.description]; }else{ return 100; } }@end
//建立
//UITableViewCell
#import <UIKit/UIKit.h>@interface RootTableViewCell : UITableViewCell@property(nonatomic,strong)UIImageView *iconImage;@property(nonatomic,strong)UILabel *nameLabel;@property(nonatomic,strong)UILabel *telLabel;@property(nonatomic,strong)UILabel *descriptionLabel;//類方法:一定是在建立cell顯示之前,知道文字的高度//計算cell 的高度//類方法是建立之前+(CGFloat)cellHeight:(NSString *)text;//計算文字的高度+(CGFloat)textHeight:(NSString *)aString;@end
#import "RootTableViewCell.h"@implementation RootTableViewCell- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{ self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { // Initialization code [self addAllViews]; } return self;}//計算cell 的高度+(CGFloat)cellHeight:(NSString *)text{ //死值和文字的高度 return 20+60+10+20+[[self class] textHeight:text];}//計算文字的高度+(CGFloat)textHeight:(NSString *)aString{ NSDictionary *dict = @{NSFontAttributeName:[UIFont systemFontOfSize:17]}; //計算文本裡面字型的高度 CGRect textRect = [aString boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - 40, 1000)options:(NSStringDrawingUsesLineFragmentOrigin) attributes:dict context:nil]; //只使用它的高度,所以返回他的高度 return textRect.size.height;}-(void)addAllViews{ //頭像圖片 self.iconImage = [[UIImageView alloc] init]; self.iconImage.frame = CGRectMake(20, 20, 60, 60); self.iconImage.backgroundColor = [UIColor redColor]; [self.contentView addSubview:self.iconImage]; //姓名label self.nameLabel = [[UILabel alloc] init]; self.nameLabel.frame = CGRectMake(CGRectGetMaxX(self.iconImage.frame) + 10, CGRectGetMinY(self.iconImage.frame), self.bounds.size.width - 40 - self.iconImage.frame.size.width - 10, 25); self.nameLabel.backgroundColor= [UIColor blueColor]; [self.contentView addSubview:self.nameLabel]; //電話label self.telLabel = [[UILabel alloc] init]; self.telLabel.frame = CGRectMake(CGRectGetMaxX(self.iconImage.frame) + 10, CGRectGetMaxY(self.nameLabel.frame) + 10, self.bounds.size.width - 40 - self.iconImage.frame.size.width - 10, 25); self.telLabel.backgroundColor= [UIColor blueColor]; self.descriptionLabel.numberOfLines = 0; [self.contentView addSubview:self.telLabel]; //簡介label self.descriptionLabel = [[UILabel alloc] init]; self.descriptionLabel.frame = CGRectMake(CGRectGetMinX(self.iconImage.frame), CGRectGetMaxY(self.iconImage.frame) + 10, self.bounds.size.width - 40, 80); self.descriptionLabel.backgroundColor = [UIColor cyanColor]; [self.contentView addSubview:self.descriptionLabel]; }- (void)awakeFromNib{ // Initialization code}- (void)setSelected:(BOOL)selected animated:(BOOL)animated{ [super setSelected:selected animated:animated]; // Configure the view for the selected state}@end
//自訂cell
#import <UIKit/UIKit.h>@interface GirlTableViewCell : UITableViewCell@property(nonatomic,strong)UIImageView *iconImage;@property(nonatomic,strong)UILabel *nameLabel;@property(nonatomic,strong)UILabel *telLabel;@end
#import "GirlTableViewCell.h"@implementation GirlTableViewCell- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{ self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { // Initialization code [self addAllViews]; } return self;}-(void)addAllViews{ //頭像圖片 self.iconImage = [[UIImageView alloc] init]; self.iconImage.frame = CGRectMake(20, 20, 60, 60); self.iconImage.backgroundColor = [UIColor yellowColor]; [self.contentView addSubview:self.iconImage]; //名字label self.nameLabel = [[UILabel alloc] init]; self.nameLabel.frame = CGRectMake(CGRectGetMaxX(self.iconImage.frame) + 10, CGRectGetMinY(self.iconImage.frame), self.bounds.size.width - 40 - self.iconImage.frame.size.width - 10, 25); self.nameLabel.backgroundColor= [UIColor greenColor]; [self.contentView addSubview:self.nameLabel]; //電話label self.telLabel = [[UILabel alloc] init]; self.telLabel.frame = CGRectMake(CGRectGetMaxX(self.iconImage.frame) + 10, CGRectGetMaxY(self.nameLabel.frame) + 10, self.bounds.size.width - 40 - self.iconImage.frame.size.width - 10, 25); self.telLabel.backgroundColor= [UIColor greenColor]; [self.contentView addSubview:self.telLabel]; }- (void)awakeFromNib{ // Initialization code}- (void)setSelected:(BOOL)selected animated:(BOOL)animated{ [super setSelected:selected animated:animated]; // Configure the view for the selected state}@end
iOS UITableView表視圖(3)自訂cell