2013-08-14 2 views
0

Существует специальная ячейка. В нем есть две метки. Один из них, названный именем nameLabel, должен динамически меняться. Иногда это может быть 1,2 или 3 строки. Но строки находятся на eachother, они пересекают свои строки строк. Как я могу решить эту проблему?Изменение динамической высоты для строк таблицы, CustomCell и макета

Этикетки и Пользовательский объект ячейки Использование опции Автоотключение отключено. Высота метки должна меняться динамически, а затем CustomCell's. А потом tableRow. Моя голова сбита с толку. И почему я не вижу, как меняется цвет фона CustomCell?

Благодаря

Вот мой код:

- (CGFloat)tableView:(UITableView *)tableView 
heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UILabel *label = [[UILabel alloc] init]; 
    label.numberOfLines = 0; // allows label to have as many lines as needed 
    label.text = [[self.dataList objectAtIndex:indexPath.row] objectForKey:@"NAME"]; 
    CGSize labelSize = [label.text sizeWithFont:label.font constrainedToSize:CGSizeMake(320, 63) lineBreakMode:NSLineBreakByWordWrapping]; 
    CGFloat h = labelSize.height; 
    NSInteger x=0.0; 
    if (h==63.0) x=30; 
    if (h==42.0) x=20; 
    if (h==21.0) x=10; 

    return h+30; 
} 

- (UITableViewCell*)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *cellID = @"cellid"; 

    CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:cellID];  
    if (cell == nil) 
    { 
     cell = (CustomCell*)[[[NSBundle mainBundle] 
           loadNibNamed:@"CustomCell" owner:nil options:nil] 
          lastObject]; 
    }  
    // customization 
    NSDictionary *d = [self.dataList objectAtIndex:indexPath.row]; 

    cell.nameLabel.text = [d objectForKey:@"NAME"]; 
    cell.cityLabel.text = [d objectForKey:@"CODE"]; 
    cell.indexPath = indexPath;  

    return cell; 
} 

В этой связи есть картина тренажере и XIb на заказ Cell, чтобы понять легко проблему: http://compfreek.wordpress.com/2013/08/14/custom-cell-for-table-row-height-changes-dynamic/

ответ

0

Hear является другой способ попробовать это изменение в соответствии с кодом ур, он может отличаться, но он может решить вашу проблему. Я помещаю все представления только через код, потому что это легко для меня проверить это.

//in your subclassed class 
    - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 
    // Initialization code 

    //may be u did same i am adding the label to cell 
    UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectZero]; 
    nameLabel.numberOfLines = 5; //set number of lines 
    nameLabel.tag = 100; 
    [self addSubview:nameLabel]; 

    UILabel *otherLabel = [[UILabel alloc] initWithFrame:CGRectZero]; 
    otherLabel.tag = 200; 
    [self addSubview:otherLabel]; 

    } 
    return self; 

} 

- (void)setSelected:(BOOL)selected animated:(BOOL)animated 
{ 
    [super setSelected:selected animated:animated]; 
    // Configure the view for the selected state 
    //do your customisation when cell is selected 
} 

-(void)layoutSubviews 
{ 
    //in this method i am setting the frames for label 

    [super layoutSubviews]; 
    UILabel *nameLabel = (UILabel *) [self viewWithTag:100]; 
    nameLabel.text = self.nameString; 


    CGSize maxSize = CGSizeMake(self.bounds.size.width/2, MAXFLOAT);//set max height 
    CGSize nameSize = [self.nameString sizeWithFont:[UIFont systemFontOfSize:17] 
         constrainedToSize:maxSize 
          lineBreakMode:NSLineBreakByWordWrapping]; 
    nameLabel.frame = CGRectMake(self.bounds.origin.x +2,self.bounds.origin.y+3, nameSize.width, nameSize.height); 

    UILabel *otherLabel = (UILabel *) [self viewWithTag:200]; 
    otherLabel.frame = CGRectMake(nameLabel.frame.size.width+15,self.bounds.origin.y+3, 100, 40); 
    otherLabel.text = self.otherString; 

} 

//in your main class 

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomCell"]; 
    if(cell == nil) 
    { 
     cell = [[[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CustomCell"]autorelease]; 
    } 

    //change it for ur need 
    NSString *name = @"hear is your long length name uzamaki naruto from uzamaki clan"; 
    NSString *other = @"other name"; 
    cell.nameString = name; 
    cell.otherString = other; 

    return cell; 
    } 

    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *name = @"hear is your long length name uzamaki naruto from uzamaki clan"; 


    //replace it your height logic 
    float cellWidth = 350; // ur cell width 
    CGSize maxSize = CGSizeMake(cellWidth/2, MAXFLOAT);//set max height 
    CGSize nameSize = [name sizeWithFont:[UIFont systemFontOfSize:17] 
           constrainedToSize:maxSize 
            lineBreakMode:NSLineBreakByWordWrapping]; 

return nameSize.height + 10;// for ur height 

} 


+0

Спасибо за ответ. Осталось только одно. Я создал ярлыки в xib. Итак, как я могу установить высоту, которая заставляет его знать изменение динамического? –

+0

Прежде всего, вам нужно установить высоту для рамки вашего ярлыка, см. Мой код, который я установил в «- (void) layoutSubviews» метод –

+0

сделал и попытался ..? –

Смежные вопросы