'셀'에 해당되는 글 3건

  1. 2013.06.25 셀 높이 조절
  2. 2013.06.12 스토리보드 커스텀 셀 (공개)
  3. 2011.10.19 UITableViewCellStyle 종류
iOS2013. 6. 25. 18:54

This sample code shows how we can create uitableviewcell height dynamically based on the text it consists.
Let assume we have UILabel in cell which contains some text, and code below  creates the appropriate cell in tableview depending on the label size :

1. Lets define some constants for the program:
      #define FONT_SIZE 14.0f
      #define CELL_CONTENT_WIDTH  305.0f
      #define CELL_CONTENT_MARGIN  8.0f

2. Return the height of each cell depending on its label size : 

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
        NSString *text =@"Read your text for each cell here (from Array or Dictionary)";
         // calculating the size of the text/string
        CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
        CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
  
         // Calculating the height of cell
              CGFloat height = MAX(size.height, 44.0f);
              return height + (CELL_CONTENT_MARGIN * 2);

}

3.  Now lets create cell having UILabel init:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] ;
    }
// Below two line code will insure that there will be no overlapping/blurr of custom views created inside cell
    for (UIView * view in cell.contentView.subviews) {
        [view removeFromSuperview];
    }


          NSString *text = @"Read your text for each cell here (from Array or Dictionary)";
          UILabel *descriptionLabel = [[UILabel alloc] initWithFrame: CGRectMake(10, 8, 278, 40)];
          // 40 choose the height of label double of normal label size
           descriptionLabel.adjustsFontSizeToFitWidth = NO;
           descriptionLabel.text = text;
             [cell.contentView addSubview:descriptionLabel];
             [descriptionLabel setNumberOfLines:0];
           CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
           CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
           [descriptionLabel setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), MAX(size.height, 44.0f))];

 return cell;
    
}

Posted by 다오나무
iOS2013. 6. 12. 16:00

파트 1

http://www.youtube.com/watch?annotation_id=annotation_989047&v=fnzkcV_XUw8&src_vid=0AsChJk422c&feature=iv


파트 2

https://www.youtube.com/watch?v=0AsChJk422c&noredirect=1

'iOS' 카테고리의 다른 글

셀 높이 조절  (0) 2013.06.25
앱 정보 가져오기 (앱이름, 버전)  (0) 2013.06.19
유니코드 출력  (0) 2013.02.01
[iPhone]keychain 을 이용한 id, password 저장  (0) 2013.01.16
[iPhone]keychain 을 이용한 id, password 저장  (0) 2012.12.18
Posted by 다오나무
영삼이의 IT정보2011. 10. 19. 10:25


Posted by 다오나무