<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                @上一節中,cell的行高是我們自己定義的,但是在實際開發過程中,我們一開始并不能預知道cell的高度,因為我們并不能知道我們寫進的數據在手機屏幕中能有多少行,如果不進行設置,則會變成下面圖示情況:(右邊的是加入了// 不限制文本行數_label.numberOfLines = 0 和 // 換行模式_label.lineBreakMode = NSLineBreakByWordWrapping; ) @因為我們一開始定義的_label的frame和cell的高度是固定值,所以就算設置了換行顯示,也沒有足夠的位置顯示出來 @正確做法代碼如下: **@HMTStudent.h** ~~~ HMTStudent.h #import <Foundation/Foundation.h> @interface HMTStudent : NSObject @property (nonatomic,copy) NSString * stuName; @property (nonatomic,copy) NSString * stuSex; @property (nonatomic,copy) NSString * stuPhoneNumber; @property (nonatomic,copy) NSString * stuIcon; @property (nonatomic,copy) NSString * stuIntroduce; - (id)initWithDictionary:(NSDictionary *)dic; @end HMTStudent.m #import "HMTStudent.h" @implementation HMTStudent - (void)dealloc{ RELEASE_SAFELY(_stuName); RELEASE_SAFELY(_stuPhoneNumber); RELEASE_SAFELY(_stuSex); RELEASE_SAFELY(_stuIcon); RELEASE_SAFELY(_stuIntroduce); [super dealloc]; } - (id)initWithDictionary:(NSDictionary *)dic{ if (self = [super init]) { self.stuName = [dic objectForKey:@"name"]; self.stuIcon = [dic objectForKey:@"icon"]; self.stuIntroduce = [dic objectForKey:@"introduce"]; self.stuPhoneNumber = [dic objectForKey:@"phoneNumber"]; self.stuSex = [dic objectForKey:@"sex"]; } return self; } @end ~~~ **@HMTMyCustomCell** ~~~ HMTMyCustomCell.h #import <UIKit/UIKit.h> @class HMTStudent; @interface HMTMyCustomCell : UITableViewCell @property (nonatomic,retain) UIImageView * iconImageView; @property (nonatomic,retain) UILabel * nameLabel; @property (nonatomic,retain) UILabel * phoneNumberLabel; @property (nonatomic,retain) UILabel * introduceLabel; // 給自定義的cell一個學生對象的屬性(接受什么數據,就給什么數據類型的屬性),重寫set方法,傳入學生對象,在自定義的cell內部完成賦值 @property (nonatomic,retain) HMTStudent * student; + (CGFloat)cellHeight:(HMTStudent *)student; @end HMTMyCustomCell.m #import "HMTMyCustomCell.h" #import "HMTStudent.h" #define IconView_MarginX 10 #define IconView_MarginY 10 #define IconView_Width 80 #define NameLabel_MarginY 10 #define NameLabel_MarginLeft 10 #define NameLabel_Width 210 #define NameLabel_Height 30 #define PhoneNumberLabel_MarginTop 10 #define PhoneNumberLabel_Width 210 #define PhoneNumberLabel_Height 30 #define IntroduceLabel_MarginTop 10 #define IntroduceLabel_Width 210 #define IntroduceLabel_Height 30 @implementation HMTMyCustomCell - (void)dealloc{ RELEASE_SAFELY(_iconImageView); RELEASE_SAFELY(_nameLabel); RELEASE_SAFELY(_phoneNumberLabel); RELEASE_SAFELY(_introduceLabel); RELEASE_SAFELY(_student); [super dealloc]; } - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { // Initialization code [self createMyCustomView]; } return self; } - (void)createMyCustomView{ self.iconImageView = [[UIImageView alloc]initWithFrame:CGRectMake(IconView_MarginX, IconView_MarginY, IconView_Width, IconView_Width)]; _iconImageView.backgroundColor = [UIColor yellowColor]; [self.contentView addSubview:_iconImageView]; [_iconImageView release]; CGFloat nameX = IconView_MarginX + IconView_Width + NameLabel_MarginLeft; self.nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(nameX, NameLabel_MarginY, NameLabel_Width, NameLabel_Height)]; _nameLabel.backgroundColor = [UIColor purpleColor]; [self.contentView addSubview:_nameLabel]; [_nameLabel release]; CGFloat phoneNumberX = nameX; CGFloat phoneNumberY = NameLabel_MarginY + NameLabel_Height +PhoneNumberLabel_MarginTop; self.phoneNumberLabel = [[UILabel alloc]initWithFrame:CGRectMake(phoneNumberX, phoneNumberY, PhoneNumberLabel_Width, PhoneNumberLabel_Height)]; _phoneNumberLabel.backgroundColor = [UIColor grayColor]; [self.contentView addSubview:_phoneNumberLabel]; [_phoneNumberLabel release]; CGFloat introdueceX = nameX; CGFloat introdueceY = phoneNumberY + PhoneNumberLabel_Height + IntroduceLabel_MarginTop; self.introduceLabel = [[UILabel alloc]initWithFrame:CGRectMake(introdueceX, introdueceY, IntroduceLabel_Width, IntroduceLabel_Height)]; _introduceLabel.backgroundColor = [UIColor greenColor]; // 行數無限制 _introduceLabel.numberOfLines = 0; // 換行模式 _introduceLabel.lineBreakMode = NSLineBreakByCharWrapping; // 和計算文本高度處,保持字體一致 _introduceLabel.font = [UIFont systemFontOfSize:15.0]; [self.contentView addSubview:_introduceLabel]; [_introduceLabel release]; } // 傳入學生對象,在自定義的cell內部完成賦值 - (void)setStudent:(HMTStudent *)student{ if (_student != student) { [_student release]; _student = [student retain]; } _iconImageView.image = [UIImage imageNamed:_student.stuIcon]; _nameLabel.text = _student.stuName; _phoneNumberLabel.text = _student.stuPhoneNumber; _introduceLabel.text = _student.stuIntroduce; // 自動調節cell高度,先計算文本高度 // 文本渲染區域,本工程的需求是固定寬度,因此高度請進來指定很大值(需求固定高度,那么寬度就是要指定很大的值) // 計算的字體大小必須和label的字體一樣 NSDictionary * attributedDic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15.0],NSFontAttributeName, nil]; CGRect newIntroduceTextRect = [_student.stuIntroduce boundingRectWithSize:CGSizeMake(_introduceLabel.frame.size.width, 20000) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributedDic context:nil]; NSLog(@"%@",NSStringFromCGRect(newIntroduceTextRect)); // 高度重新設置 CGRect oldIntroduceTextRect = _introduceLabel.frame; oldIntroduceTextRect.size.height = newIntroduceTextRect.size.height; _introduceLabel.frame = oldIntroduceTextRect; } + (CGFloat)cellHeight:(HMTStudent *)student{ NSDictionary * attributedDic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15.0],NSFontAttributeName]; CGRect newIntroduceTextRect = [student.stuIntroduce boundingRectWithSize:CGSizeMake(IntroduceLabel_Width, 20000) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributedDic context:nil]; // 高度重新設置 return newIntroduceTextRect.size.height + PhoneNumberLabel_MarginTop*3 + NameLabel_Height + PhoneNumberLabel_Height; } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state } @end ~~~ **@HMTCustomViewController** ~~~ #import "HMTCustomViewController.h" #import "HMTStudent.h" #import "HMTMyCustomCell.h" #import "HMTGirCustomlCell.h" @interface HMTCustomViewController () @property (nonatomic,retain) NSMutableArray * allStudentArray; @end @implementation HMTCustomViewController - (void)dealloc{ RELEASE_SAFELY(_allStudentArray); [super dealloc]; } - (id)initWithStyle:(UITableViewStyle)style { self = [super initWithStyle:style]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Uncomment the following line to preserve selection between presentations. // self.clearsSelectionOnViewWillAppear = NO; // Uncomment the following line to display an Edit button in the navigation bar for this view controller. self.navigationItem.rightBarButtonItem = self.editButtonItem; self.navigationItem.title = @"所有聯系人"; self.tableView.separatorColor = [UIColor redColor]; // 獲取本地文件路徑 NSString * filePath = [[NSBundle mainBundle]pathForResource:@"Students" ofType:@"plist"]; NSArray * originArray = [NSArray arrayWithContentsOfFile:filePath]; // self.allStudentArray = [NSMutableArray array]; for (int i = 0; i < [originArray count]; i++ ) { NSDictionary * stuDic = [originArray objectAtIndex:i]; HMTStudent * student = [[HMTStudent alloc]initWithDictionary:stuDic]; [_allStudentArray addObject:student]; [student release]; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [_allStudentArray count]; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return [HMTMyCustomCell cellHeight:[_allStudentArray objectAtIndex:indexPath.row]] + 10; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { HMTStudent * student = [_allStudentArray objectAtIndex:indexPath.row]; if ([student.stuSex isEqualToString:@"女"]) { static NSString * CellIdentifier = @"GirlCell"; HMTGirCustomlCell * girlCell = (HMTGirCustomlCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (!girlCell) { girlCell = [[[HMTGirCustomlCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease]; } girlCell.student = student; return girlCell; } static NSString * Identifier = @"BoyCell"; HMTMyCustomCell * boyCell = (HMTMyCustomCell *)[tableView dequeueReusableCellWithIdentifier:Identifier]; if (!boyCell) { boyCell = [[[HMTMyCustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identifier]autorelease]; } boyCell.student = student; return boyCell; } ~~~ @最后效果如下: *![](https://box.kancloud.cn/2016-01-12_5694d751db566.jpg)*
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看