UITable вид перекрытия - PullRequest
       2

UITable вид перекрытия

1 голос
/ 11 января 2011
- (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] autorelease];
    }

    // Configure the cell.

    Book *aBook = [appDelegate.books objectAtIndex:indexPath.row];
    UILabel *myLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, 300, 22)];
    UILabel *myLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 40, 300, 22)];
    UILabel *myLabel3 = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, 300, 22)];
    myLabel1.text=aBook.title;
    myLabel2.text=aBook.description;
    myLabel3.text=aBook.pubDate;

    [cell addSubview:myLabel1];
    [cell addSubview:myLabel2];
    [cell addSubview:myLabel3];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    // Set up the cell

    return cell;
}

У меня есть этот код.Отображается из файла XML.Когда я прокручиваю, текст перекрывается.Пожалуйста, помогите мне.

1 Ответ

3 голосов
/ 11 января 2011

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

- (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] autorelease];

        UILabel *myLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, 300, 22)];
        UILabel *myLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 40, 300, 22)];
        UILabel *myLabel3 = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, 300, 22)];
        myLabel1.tag = 101;
        myLabel2.tag = 102;
        myLabel3.tag = 103;

        [cell.contentView addSubview:myLabel1];
        [cell.contentView addSubview:myLabel2];
        [cell.contentView addSubview:myLabel3];
        [myLabel1 release];
        [myLabel2 release];
        [myLabel3 release];
    }

    // Configure the cell.

    Book *aBook = [appDelegate.books objectAtIndex:indexPath.row];
    UILabel *myLabel1 = (UILabel*)[cell.contentView viewWithTag:101];
    UILabel *myLabel2 = (UILabel*)[cell.contentView viewWithTag:101];
    UILabel *myLabel3 = (UILabel*)[cell.contentView viewWithTag:101];
    myLabel1.text=aBook.title;
    myLabel2.text=aBook.description;
    myLabel3.text=aBook.pubDate;

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    // Set up the cell

    return cell;
}

Еще две вещи, которые нужно исправить:

  • Не забудьте освободить ярлыки, созданные последобавив их в ячейку, в противном случае вы получите утечку памяти и, возможно, в конечном итоге столкнетесь с проблемами нехватки памяти (особенно при просмотре таблицы)
  • добавьте подпредставления в contentView ячейки, а не непосредственно в ячейку
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...