Obj-C: UIImageview в Tableviewcell продолжают появляться - PullRequest
0 голосов
/ 04 февраля 2011

Я работаю над проектом для iPhone, который показывает обложку в верхней части таблицы (раздел 0) и показывает список элементов в (раздел 1). вид таблицы выглядит так:

section0:

ARTWORK IMAGE

section1:

ячейка1: текст1

ячейка2: текст2

. , , и так поехали.

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

вот код моей программы:

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


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
switch (section) {
    case 0:
        return 1;
        break;
    case 1:
        return [diskArray count];
        break;
    default:
        return 0;
        break;
}
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

switch (indexPath.section) {
    case 0:
        return 225;
        break;
    default:
        return 44;
        break;
}

}
// Customize the appearance of table view cells.
- (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];
}
// Configure the cell...
if(indexPath.section == 0)
{
    if(indexPath.row == 0)
    {
    NSLog(@"adding image to the cell");
    UIImage *img = [UIImage imageNamed:@"artwork.jpeg"];
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(47, 0, 225, 225)];
    imgView.image = img;
    [cell addSubview:imgView];
    [imgView release];
    cell.backgroundColor = [UIColor clearColor];
    cell.textLabel.text = nil;
    }
}
else if(indexPath.section == 1){
    cell.textLabel.text = [diskArray objectAtIndex:indexPath.row];
    cell.backgroundColor = [UIColor darkGrayColor];
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}

return cell;
}

Ответы [ 2 ]

1 голос
/ 04 февраля 2011

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

0 голосов
/ 04 февраля 2011

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

if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
} else {
    [cell subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
    //you should also reinit the other values, as textLabel, bgColor et al.
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...