UICell проблема iPhone во время прокрутки tableView - PullRequest
0 голосов
/ 08 августа 2009

Моя текущая проблема Связанные ссылки - одна из ниже.


http://www.freeimagehosting.net/image.php?a65dae5f4a.png>http://www.freeimagehosting.net/uploads/th.a65dae5f4a.png alt = "Бесплатный хостинг изображений от FreeImageHosting.net">

[URL = http://www.freeimagehosting.net/image.php?a65dae5f4a.png][img]http://www.freeimagehosting.net/uploads/th.a65dae5f4a.png[/img][/url]

[URL = http://www.freeimagehosting.net/image.php?a65dae5f4a.png][img=http://www.freeimagehosting.net/uploads/th.a65dae5f4a.png][/url]

http://www.freeimagehosting.net/>http://www.freeimagehosting.net/uploads/a65dae5f4a.png border = 0 alt = "Бесплатный хостинг изображений">

[URL = http://www.freeimagehosting.net/][img]http://www.freeimagehosting.net/uploads/a65dae5f4a.png[/img][/url]

[URL = http://www.freeimagehosting.net/][img=http://www.freeimagehosting.net/uploads/a65dae5f4a.png][/url]


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

enter code here

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

// customizing cell
student *t=[studentsList objectAtIndex:indexPath.row];
cell.backgroundColor=[UIColor lightGrayColor];
CGRect label1Frame=CGRectMake(10, 10, 290, 25);
CGRect label2Frame=CGRectMake(10, 33, 290, 25);
CGRect label3Frame=CGRectMake(10, 56, 290, 25);
UILabel *lbltmp;
lbltmp=[[UILabel alloc] initWithFrame:label1Frame];  // name
lbltmp.font=[UIFont boldSystemFontOfSize:15];
lbltmp.backgroundColor=[UIColor clearColor];
lbltmp.textColor=[UIColor blackColor];
lbltmp.text=t.stuName;
[cell.contentView addSubview:lbltmp];
[lbltmp release];

lbltmp=[[UILabel alloc] initWithFrame:label2Frame]; // roll no & address
lbltmp.font=[UIFont systemFontOfSize:13];
lbltmp.backgroundColor=[UIColor clearColor];
lbltmp.text=[NSString stringWithFormat:@"%i - %@",t.stuNo,t.stuAddress];
lbltmp.textColor=[UIColor grayColor];
[cell.contentView addSubview:lbltmp];
[lbltmp release];

lbltmp=[[UILabel alloc] initWithFrame:label3Frame]; // city & pin
lbltmp.font=[UIFont systemFontOfSize:13];
lbltmp.backgroundColor=[UIColor clearColor];
lbltmp.text=[NSString stringWithFormat:@"%@ - %@",t.stuCity,t.stuPin];
lbltmp.textColor=[UIColor grayColor];
[cell.contentView addSubview:lbltmp];
[lbltmp release];
//----------------------------------
cell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton;
return cell;

}

Если я реализую код, как описано выше, проблема возникает, как описано на изображениях выше.

Если я реализую следующий код, никаких проблем не возникает.

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

// customizing cell
student *t=[studentsList objectAtIndex:indexPath.row];
cell.backgroundColor=[UIColor lightGrayColor];
CGRect label1Frame=CGRectMake(10, 10, 290, 25);
CGRect label2Frame=CGRectMake(10, 33, 290, 25);
CGRect label3Frame=CGRectMake(10, 56, 290, 25);
UILabel *lbltmp;
lbltmp=[[UILabel alloc] initWithFrame:label1Frame];  // name
lbltmp.font=[UIFont boldSystemFontOfSize:15];
lbltmp.backgroundColor=[UIColor clearColor];
lbltmp.textColor=[UIColor blackColor];
lbltmp.text=t.stuName;
[cell.contentView addSubview:lbltmp];
[lbltmp release];

lbltmp=[[UILabel alloc] initWithFrame:label2Frame]; // roll no & address
lbltmp.font=[UIFont systemFontOfSize:13];
lbltmp.backgroundColor=[UIColor clearColor];
lbltmp.text=[NSString stringWithFormat:@"%i - %@",t.stuNo,t.stuAddress];
lbltmp.textColor=[UIColor grayColor];
[cell.contentView addSubview:lbltmp];
[lbltmp release];

lbltmp=[[UILabel alloc] initWithFrame:label3Frame]; // city & pin
lbltmp.font=[UIFont systemFontOfSize:13];
lbltmp.backgroundColor=[UIColor clearColor];
lbltmp.text=[NSString stringWithFormat:@"%@ - %@",t.stuCity,t.stuPin];
lbltmp.textColor=[UIColor grayColor];
[cell.contentView addSubview:lbltmp];
[lbltmp release];
//----------------------------------
cell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton;
}
return cell;

}

Реализация вышеуказанного кода означает воссоздание каждой ячейки таблицы, пока пользователь прокручивает tableView. Это может замедлить ваше приложение, и это не правильное решение,

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

Ответы [ 2 ]

1 голос
/ 08 августа 2009

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

Один из способов исправить это: подкласс UITableViewCell и присвоить ему указатели UILabel s для текста, который вы хотите изменить. Затем, когда ячейка будет переработана, вы можете просто получить доступ к этим свойствам вместо повторного добавления новых подпредставлений.

Ссылка: Документ Apple на ячейки табличного представления .

0 голосов
/ 09 августа 2009

Я думаю, что решение вашей проблемы заключается в следующем:

Реализуйте следующий метод для tableViewCotroller

-(UITableViewCell*)getCellContentView:(NSString*)cellIdentifier
{
}

Например, я реализовал следующее

-(UITableViewCell*)getCellContentView:(NSString*)cellIdentifier
{
    CGRect photoFrame=CGRectMake(5, 5, 60, 60);
    CGRect label1Frame=CGRectMake(90, 10, 290, 25);
    CGRect label2Frame=CGRectMake(90, 30, 290, 25);
    CGRect label3Frame=CGRectMake(90, 50, 290, 25);

    UITableViewCell *cell=[[[UITableViewCell alloc] initWithFrame:CGRectMake(0, 0, 300, 80) reuseIdentifier:cellIdentifier] autorelease];

    UILabel *tmp;
    tmp=[[UILabel alloc] initWithFrame:label1Frame];
    tmp.tag=1;
    tmp.textColor=[UIColor blackColor];
    tmp.font=[UIFont boldSystemFontOfSize:15];
    [cell.contentView addSubview:tmp];
    [tmp release];

    tmp=[[UILabel alloc] initWithFrame:label2Frame];
    tmp.tag=2;
    tmp.adjustsFontSizeToFitWidth=0;
    tmp.textColor=[UIColor grayColor];
    tmp.font=[UIFont systemFontOfSize:13];
    [cell.contentView addSubview:tmp];
    [tmp release];

    tmp=[[UILabel alloc] initWithFrame:label3Frame];
    tmp.tag=3;
    tmp.adjustsFontSizeToFitWidth=0;
    tmp.textColor=[UIColor grayColor];
    tmp.font=[UIFont systemFontOfSize:13];
    [cell.contentView addSubview:tmp];
    [tmp release];

    UIImageView *imgView=[[UIImageView alloc]initWithFrame:photoFrame];
    imgView.tag=4;
    [cell.contentView addSubview:imgView];
    [imgView release];

    return cell;
}

Я также назвал метод выше формы cellForRowAtIndexPath

пример

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
    {
        cell = [self getCellContentView:CellIdentifier];
    }
    // customizing cell
    student *t=[studentsList objectAtIndex:indexPath.row];
    cell.backgroundColor=[UIColor lightGrayColor];

    UILabel *lb1=(UILabel*)[cell viewWithTag:1];
    UILabel *lb2=(UILabel*)[cell viewWithTag:2];
    UILabel *lb3=(UILabel*)[cell viewWithTag:3];

    NSString *dtlOne=[NSString stringWithFormat:@"Enr.No - %i, %@",t.stuNo,t.stuAddress];
    NSString *dtlTwo=[NSString stringWithFormat:@"%@ - %@.",t.stuCity,t.stuPin];
    lb1.text=t.stuName;
    lb2.text=dtlOne;
    lb3.text=dtlTwo;    

    cell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton;

    return cell;
}

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

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