Метка в UITableCellView.contentView отображается два раза при использовании авторазмера - PullRequest
0 голосов
/ 19 декабря 2011

Я использую Grouped UITableView и добавляю UITableViewCells две UILabel's и добавляю их в contentView ячейки.Я также реализую режим редактирования в моем приложении на этом UITableView.

Я использую lblDetail.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;, чтобы уменьшить ширину текста, чтобы красная кнопка удаления, отображаемая в режиме редактирования, не закрывала мой текст.

Теперь я вижу, когда я прокручиваютаблица не находится в режиме редактирования, метки отображаются два раза, как если бы она находилась в режиме редактирования и одновременно в режиме редактирования.

Это код:

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

    if (cell == nil) {

        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
    }


    CGRect DetailLabelFrame = CGRectMake(52, 25, 200, 35);
    CGRect TitleLabelFrame = CGRectMake(52, 5, 200, 25);

    lblDetail = [[UILabel alloc]init];
    lblTitle = [[UILabel alloc]init];

    lblDetail.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;
    lblTitle.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;

    lblDetail.frame = DetailLabelFrame;
    lblDetail.lineBreakMode = UILineBreakModeWordWrap;
    lblDetail.backgroundColor = [UIColor clearColor];
    lblDetail.numberOfLines = 2;

    lblTitle.frame = TitleLabelFrame;    
    lblTitle.backgroundColor = [UIColor clearColor];
    lblTitle.frame = TitleLabelFrame;

    [lblDetail setFont:[UIFont fontWithName:@"Arial" size:14]];
    [lblTitle setFont:[UIFont fontWithName:@"Arial-BoldMT"  size:15]];

    lblDetail.textAlignment = UITextAlignmentRight;
    lblTitle.textAlignment = UITextAlignmentRight;

    lblTitle.text = @"054-9700583"; 
    lblDetail.text = @"שאלה:במה אפשר לעזור לך?";

   [cell.contentView addSubview:lblDetail];
   [cell.contentView addSubview:lblTitle];

    cell.imageView.layer.masksToBounds = YES;
    cell.imageView.layer.cornerRadius = 5.0;
    cell.imageView.image = [UIImage imageNamed:@"girl.png"];

    [lblDetail release];
    [lblTitle release];

    return cell;
}

Ответы [ 2 ]

1 голос
/ 20 декабря 2011
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {

    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];


CGRect DetailLabelFrame = CGRectMake(52, 25, 200, 35);
CGRect TitleLabelFrame = CGRectMake(52, 5, 200, 25);

lblDetail = [[UILabel alloc]init];
lblTitle = [[UILabel alloc]init];

lblDetail.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;
lblTitle.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;

lblDetail.frame = DetailLabelFrame;
lblDetail.lineBreakMode = UILineBreakModeWordWrap;
lblDetail.backgroundColor = [UIColor clearColor];
lblDetail.numberOfLines = 2;

lblTitle.frame = TitleLabelFrame;    
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.frame = TitleLabelFrame;

[lblDetail setFont:[UIFont fontWithName:@"Arial" size:14]];
[lblTitle setFont:[UIFont fontWithName:@"Arial-BoldMT"  size:15]];

lblDetail.textAlignment = UITextAlignmentRight;
lblTitle.textAlignment = UITextAlignmentRight;
[cell.contentView addSubview:lblDetail];
[cell.contentView addSubview:lblTitle];

lblDetail.tag = 10;
lblTitle.tag = 11;

}


lblDetail = (UILabel*)[cell.contentView viewWithTag:10];

lblTitle = (UILabel*)[cell.contentView viewWithTag:11];

lblTitle.text = @"054-9700583"; 
lblDetail.text = @"שאלה:במה אפשר לעזור לך?";

cell.imageView.layer.masksToBounds = YES;
cell.imageView.layer.cornerRadius = 5.0;
cell.imageView.image = [UIImage imageNamed:@"girl.png"];

[lblDetail release];
[lblTitle release];

return cell;
}

Содержимое вашей ячейки перекрывалось, возможно, оно вам поможет. До сих пор, если вы столкнулись с той же проблемой, постучите ко мне без колебаний.

1 голос
/ 20 декабря 2011

Если это происходит только при прокрутке, это может произойти из-за того, что при повторном использовании ячейки создается новый набор меток в дополнение к тому, что уже находится в повторно используемой ячейке.

Что можно попробоватьсделать, это переместить эти две строки в ваш блок if (cell == nil):

lblDetail = [[UILabel alloc]init];
lblTitle = [[UILabel alloc]init];

Таким образом, когда ячейка используется повторно, она будет продолжать использовать существующие lblDetail и lblTitle вместо созданияновый.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...