2012-09-13 4 views
0

달성하고자하는 것은 이와 같습니다. 그 종류의 페이스 북이나 트위터 기능 어디 스레드와 스레드가있는 테이블 뷰를 가지고 기사의 다른 번호가 포함되어 있습니다. 기사의 수는 각 행마다 다릅니다. 그래서 기본적으로 페이스 북에 게시물을 보내고 사람들이 해당 게시물에 응답하고 그 게시물이 해당 스레드 아래에 추가됩니다 (나는 그저 모든 것을 표시하는 방법에 대해 신경을 썼습니다.) 그림 enter image description hereiOS에서 Tableview 용 동적 TableCell을 만드는 방법

나는 세포와 모든 것을 만드는 법을 안다. 그러나 나는 그 크기를 동적으로 설정하는 법을 모른다. 어떤 자습서 또는 이것을 달성하기 위해 조언의 조각 ?? 어떤 도움에 감사드립니다

..

감사

답변

1

아마도 도움이 될 것입니다. 나는 당신에게 내 코드를 보내고 있는데, 첫 번째 색인에 Emial, 두 번째 색인에 PhoneNumber, 세 번째 색인에 AddressNode (사례 기사에서)가있는 Custom Cell을 사용하고있다. 나는 동적으로 높이 CellForRowAtIndexPath의 주소 라벨의 높이를 변경하고, 또한 높이 heightForRowAtIndexPath의 셀 높이를 변경합니다. 여기 내 코드가있다.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSString *identifier = @"FeedCell"; 
    FeedDetailCell *cell = (FeedDetailCell*)[tableView dequeueReusableCellWithIdentifier:identifier]; 
    if (cell==nil) { 
     cell = (FeedDetailCell*)[[[NSBundle mainBundle] loadNibNamed:@"FeedDetail" owner:nil options:nil] objectAtIndex:0]; 
    } 
    [tableView setSeparatorColor:[UIColor grayColor]]; 
    switch (indexPath.section) { 
     case 0: 
      if (indexPath.row == 0) { 
       cell.nameLabel.text = @"Telephone"; 
       [cell.detailLabel setText:[_feedDictionary valueForKey:@"mobile"]]; 
      } 
      else { 
       cell.nameLabel.text = @"Mobile"; 
       [cell.detailLabel setText:[_feedDictionary valueForKey:@"iPhone"]]; 
      } 
      break; 
     case 1: 
      cell.nameLabel.text = @"E-mail"; 
      [cell.detailLabel setText:[_feedDictionary valueForKey:@"Email"]]; 
      break; 
     case 2: 
      cell.nameLabel.text = @"address"; 
      [cell.detailLabel setText:[_feedDictionary valueForKey:@"address"]]; 
      CGSize size = [[_feedDictionary valueForKey:@"address"] sizeWithFont:[UIFont systemFontOfSize:14.0] 
           constrainedToSize:CGSizeMake(200.0, 400.0) lineBreakMode:UILineBreakModeWordWrap]; 
      CGRect frm = cell.detailLabel.frame; 
      frm.size.height = size.height; 
      [cell.detailLabel setFrame:frm]; 
     default: 
      break; 
    } 
    return cell; 
} 


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (indexPath.section == 2) { 
     NSString *address = [_feedDictionary valueForKey:@"address"]; 
     CGSize recommendedSize = [address sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(320, INT_MAX)]; 
     return 44 + recommendedSize.height; 
    } 
    else { 
     return 44; 
    } 
} 
+0

목적을 해결해 주신 덕분에 .. – MayurCM

+0

환영 인사 ...... –

1

당신은 당신이 스위치 케이스를 사용할 수 있습니다

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath; 

동적 셀 높이를 위해 이것을 사용하고 indexPath.rowindexPath를 확인할 수 있습니다. 섹션 필요한 높이 반환

+0

도움을 주셔서 감사합니다.하지만 조금 자세히 설명해 주실 수 있습니까? 아니면 내가 따를 수있는 모범 사례를 제안 해주십시오. – MayurCM

1

take ac ustom있는 UITableViewCell,하는 .m 파일에

@interface PartnerCell : UITableViewCell { 
     UILabel *user_name,*lbldate,*lbldesc; 
     LoadImage *img_trade; 
     UIImageView *partnerimage; 
    } 
    @property (nonatomic, strong) UILabel *user_name,*lbldate,*lbldesc; 
    @property (nonatomic, strong) LoadImage *img_trade; 
    @property (nonatomic, strong) UIImageView *partnerimage; 
    @end 

.H 파일 "PartnerCell.h"기본 테이블 뷰 클래스

@implementation PartnerCell 
@synthesize user_name,lbldate,lbldesc; 
@synthesize img_trade,partnerimage; 

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { 
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) { 

     [self setSelectionStyle:UITableViewCellSelectionStyleNone]; 

     user_name = [[UILabel alloc] initWithFrame:CGRectMake(75,8,200,15)]; 
     [user_name setBackgroundColor:[UIColor clearColor]]; 
     user_name.font = [UIFont fontWithName:@"Arial-BoldMT" size:15]; 
     [user_name setTextColor:[UIColor colorWithRed:70/255.00f green:70/255.00f blue:70/255.00f alpha:1.0]]; 
     [self addSubview:user_name]; 

     lbldate = [[UILabel alloc] initWithFrame:CGRectMake(75,28,200,15)]; 
     [lbldate setBackgroundColor:[UIColor clearColor]]; 
     lbldate.font = [UIFont fontWithName:@"Arial" size:14]; 
     [lbldate setTextColor:[UIColor darkGrayColor]]; 
     [self addSubview:lbldate]; 


     lbldesc = [[UILabel alloc] initWithFrame:CGRectMake(75,45,170,35)]; 
     [lbldesc setBackgroundColor:[UIColor clearColor]]; 
     lbldesc.font = [UIFont fontWithName:@"Arial" size:13]; 
     lbldesc.numberOfLines = 2; 
     [lbldesc setTextColor:[UIColor darkGrayColor]]; 
     [self addSubview:lbldesc]; 


     img_trade = [[LoadImage alloc] initWithFrame:CGRectMake(3, 5, 54, 55)]; 
     img_trade.userInteractionEnabled = YES; 
     [self addSubview:img_trade]; 



    } 
    return self; 
} 

이 코드를 작성

# import를,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

NSString *CellIdentifier = [NSString stringWithFormat:@"%@",[[Partarray objectAtIndex:indexPath.row-1] valueForKey:@"part_id"]]; 
cell = (PartnerCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
cell=nil; 
if (cell == nil) 
{ 
    cell = [[PartnerCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    //cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[Partarray objectAtIndex:indexPath.row]] autorelease]; 
    cell.selectionStyle= UITableViewCellSelectionStyleGray; 
    cell.backgroundColor = [UIColor clearColor]; 
    cell.user_name.frame=CGRectMake(75,8,340,15); 
    cell.lbldate.frame=CGRectMake(75,28,340,15); 
    cell.lbldesc.frame=CGRectMake(75,45,340,35); 


    cell.user_name.text = @"user name"; 
    cell.lbldate.text = @"date"; 
    cell.lbldesc.text = @"description"; 



} 
    return cell; 
} 

조건을 취하고 해당 조건에 따라 개체 수를 추가하십시오.

관련 문제