2013-03-10 91 views
0

我正在嘗試向我的標籤添加一些文本。一旦文字大於單元格的指定高度。我希望細胞也能長高。我確實添加了下面的代碼來照顧我的標籤高度,但我不知道如何將其傳播到我的單元格。動態更改原型單元格的高度

代碼

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    self.navigationItem.title = @"Scribbles"; 
    //Array 
    Scribble *scribble1 = [Scribble new]; 
    [scribble1 setTitle:@"Test 1" andBody:@"This is fun! This is fun! This is fun! This is fun! This is fun! This is fun! This is fun! This is fun! This is fun! This is fun! This is fun!" andImage:@"scribble.png"]; 
    Scribble *scribble2 = [Scribble new]; 
    [scribble2 setTitle:@"Test 2" andBody:@"This is fun!" andImage:@"scribble2.png"]; 
    Scribble *scribble3 = [Scribble new]; 
    [scribble3 setTitle:@"Test 3" andBody:@"This is fun!" andImage:@"scribble3.png"]; 
    Scribble *scribble4 = [Scribble new]; 
    [scribble4 setTitle:@"Test 4" andBody:@"This is fun!" andImage:@"scribble.png"]; 
    Scribble *scribble5 = [Scribble new]; 
    [scribble5 setTitle:@"Test 5" andBody:@"This is fun!" andImage:@"scribble2.png"]; 
    Scribble *scribble6 = [Scribble new]; 
    [scribble6 setTitle:@"Test 6" andBody:@"This is fun!" andImage:@"scribble3.png"]; 
    Scribble *scribble7 = [Scribble new]; 
    [scribble7 setTitle:@"Test 7" andBody:@"This is fun!" andImage:@"scribble.png"]; 
    Scribble *scribble8 = [Scribble new]; 
    [scribble8 setTitle:@"Test 8" andBody:@"This is fun!" andImage:@"scribble3.png"]; 

    scribbles = [NSArray arrayWithObjects: 
       scribble1,scribble2,scribble3,scribble4,scribble5,scribble6,scribble7,scribble8, nil]; 
} 

//To make the Selection disappear 
- (void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:animated]; 
    [self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES]; 

} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return scribbles.count; 
} 

//Loading stuff into tableView 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *simpleTableIdentifier = @"ScribbleCell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 

    if (cell == nil){ 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier]; 
    } 

    Scribble *scribble = [scribbles objectAtIndex:indexPath.row]; 

    UIImageView *scribbleImageView = (UIImageView *)[cell viewWithTag:100]; 
    scribbleImageView.image = [UIImage imageNamed:scribble.image]; 
    scribbleImageView.layer.cornerRadius = 18.0; 
    scribbleImageView.clipsToBounds = YES; 

    UILabel *scribbleNameLabel = (UILabel *)[cell viewWithTag:101]; 
    scribbleNameLabel.text = scribble.title; 

    UILabel *scribbleBodyLabel = (UILabel *)[cell viewWithTag:102]; 
    [scribbleBodyLabel setLineBreakMode:NSLineBreakByWordWrapping]; 
    scribbleBodyLabel.numberOfLines = 0; 
    scribbleBodyLabel.text = scribble.body; 
    [scribbleBodyLabel sizeToFit]; 
    return cell; 
} 
+0

你可以分享更多的代碼嗎?有一些問題需要澄清,比如更新單元運行時並重新加載表視圖?或者您有標籤的參考,並且您做出了更改? – mangesh 2013-03-10 05:16:23

+0

@mangesh請看編輯 – Jonathan 2013-03-10 05:21:04

回答

1

如果您使用的是原型單元格,則dequeueReusableCellWithIdentifier:如果標識符正確,則每次都返回一個單元格。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *simpleTableIdentifier = @"ScribbleCell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 

    Scribble *scribble = scribble[indexPath.row]; 

    UIImageView *scribbleImageView = (UIImageView *)[cell viewWithTag:100]; 
    scribbleImageView.image = [UIImage imageNamed:scribble.image]; 
    scribbleImageView.layer.cornerRadius = 18.0; 
    scribbleImageView.clipsToBounds = YES; 

    UILabel *scribbleNameLabel = (UILabel *)[cell viewWithTag:101]; 
    scribbleNameLabel.text = scribble.title; 

    UILabel *scribbleBodyLabel = (UILabel *)[cell viewWithTag:102]; 
    [scribbleBodyLabel setLineBreakMode:NSLineBreakByWordWrapping]; 
    scribbleBodyLabel.numberOfLines = 0; 
    scribbleBodyLabel.text = scribble.body; 
    return cell; 
} 

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     Scribble *scribble = scribble[indexPath.row]; 

     NSString *scribbleBody = scribble.body; 
     UIFont *cellFont = [UIFont systemFontOfSize:17.0f]; 
     /*Instead of hardcoded width you can find width from tableView frame 
     imageView and their offsets 20 for tableView ,40 for imageView, 30 for 
     offset of body label */ 
     CGFloat width = tableView.frame.size.width - 90.0f; 
     CGSize constraintSize = CGSizeMake(width, MAXFLOAT); 
     CGSize labelSize = [scribbleBody sizeWithFont:cellFont 
            constrainedToSize:constraintSize 
             lineBreakMode:NSLineBreakByWordWrapping]; 

     //Add the height of scribbleNameLabel +offset 
     return labelSize.height + 40.0f; 
    } 
+0

Anupdas,什麼是systemFontWithSize? – Jonathan 2013-03-10 05:37:51

+0

@喬納森,這是一個錯字。編輯。 – Anupdas 2013-03-10 05:39:18

+0

:)是的,我有點想。也不應該使用NSLineBreakByWordWrapping而不是UILineBreakModeWordWrap?因爲它在iOS 6中已棄用? – Jonathan 2013-03-10 05:41:35

0

您需要使用委託方法heightForRowAtIndexPathUITableView,在這裏你可以根據你的UITableViewCell 可以follow the thread,它會幫助你對你的情況調整文本高度

+0

nsgulliver,我有額外的標籤添加到單元格中,所以cell.textLabel似乎並不適用於我 – Jonathan 2013-03-10 02:48:02

+0

@Jonathan由於您已添加scribbleBodyLabel,您需要按照線程和根據該標籤調整單元格大小。 – Anupdas 2013-03-10 04:25:31

+0

@Anupdas可以詳細說明一下嗎? – Jonathan 2013-03-10 04:28:22