3

糟糕,我正面临一个表格视图单元格没有图像的问题,即具有默认图像的单元格。滚动时默认图像消失,单元格中的一些图像出现在其上。我已经实施了 Mr.Rckoenes 的建议。即使那样我也无法解决这个问题。这是我理解的实现代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{    
    ReminderClass *reminderToDisplay = [self.remindersArray objectAtIndex:indexPath.row];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier];

    // Now create the cell to display the data
    if (cell == nil) 
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellIdentifier] autorelease];
        cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
        cell.textLabel.numberOfLines = 0;
        cell.textLabel.font = [UIFont fontWithName:kHelvetica size:17.0];
        cell.textLabel.adjustsFontSizeToFitWidth = YES;
        cell.backgroundColor = [UIColor clearColor];
    }

    ......

    if (Image != nil) 
    {
        UIImageView *imageView=[[UIImageView alloc] initWithFrame:CGRectMake(240, 3, 70, 63)];
        imageView.backgroundColor=[UIColor clearColor];
        [imageView setImage:Image];
        cell.accessoryView = imageView;
        [imageView release];
    }
    else
    {
        UIImageView *imageView=[[UIImageView alloc] initWithFrame:CGRectMake(240, 3, 70, 63)];
        imageView.backgroundColor=[UIColor clearColor];
        UIImage *defaultImage = [UIImage imageNamed:kDefaultImage];
        [imageView setImage:defaultImage];
        cell.accessoryView = imageView;
        [imageView release];
    }
    cell.textLabel.text = reminderDetailsString;
    return cell;
}

任何人都可以帮助我,在此先感谢:)

4

1 回答 1

4

刚刚设置dequeueReusableCellWithIdentifier并喜欢下面nil的..reuseIdentifiernil

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];

并在此if (cell == nil)if 条件下添加您的其他代码..

更新:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d%d",indexPath.section,indexPath.row]; 
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
        cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
        cell.textLabel.numberOfLines = 0;
        cell.textLabel.font = [UIFont fontWithName:kHelvetica size:17.0];
        cell.textLabel.adjustsFontSizeToFitWidth = YES;
        cell.backgroundColor = [UIColor clearColor];

        tableView.backgroundColor = [UIColor clearColor];

    NSDateFormatter *dateFormat = [[[NSDateFormatter alloc]init]autorelease];
    [dateFormat setDateFormat:kDateFormat];
    NSDate *reminderDate = [dateFormat dateFromString:reminderToDisplay.Date]; 
    [dateFormat setDateFormat:kMinDateFormat]; 
    NSString *dateString = [dateFormat stringFromDate:reminderDate];

    NSString *valueString = [NSString stringWithFormat:kNameEvent,reminderToDisplay.Name,reminderToDisplay.Event];
    NSString *onString = [NSString stringWithFormat:kOn,dateString];
    NSString *reminderDetailsString = [valueString stringByAppendingString:onString];

    ABAddressBookRef addressbook = ABAddressBookCreate();
    CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressbook);
    CFIndex numPeople = ABAddressBookGetPersonCount(addressbook);
    for (int i=0; i < numPeople; i++)
    {
        ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);

        NSString *firstName=(NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
        NSString *lastName=(NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);

        NSMutableDictionary *contactsDictionary = [[[NSMutableDictionary alloc]init]autorelease];

        if(firstName != nil && firstName != NULL)
        {
            [contactsDictionary setObject:firstName forKey:kFirstName];
            CFRelease(firstName);
        }
        else
        {
            [contactsDictionary setObject:@"" forKey:kFirstName];
        }
        if(lastName != nil && lastName != NULL)
        {
            [contactsDictionary setObject:lastName forKey:kLastName];
            CFRelease(lastName);
        }
        else
        {
            [contactsDictionary setObject:@"" forKey:kLastName];
        }

        //Get the first name and last name added to dict and combine it to form contact name
        firstName = [[[NSString alloc]initWithString:[contactsDictionary objectForKey:kFirstName]]autorelease];
        lastName = [NSString stringWithFormat:@" %@",[contactsDictionary objectForKey:kLastName]];
        self.contactName = [firstName stringByAppendingString:lastName];

        //Now check whether the contact name is same as your reminderToDisplay.Name
        if([reminderToDisplay.Name isEqualToString:contactName] && ABPersonHasImageData(person))
        {
            CFDataRef imageData = ABPersonCopyImageData(person);
            self.reminderImage = [UIImage imageWithData:(NSData *)imageData];
            CFRelease(imageData);
        }
    }

    CFRelease(allPeople);
    CFRelease(addressbook);

    if ([reminderToDisplay.reminderGroup isEqualToString:kFacebook])
    {
        NSString *imageName = [NSString stringWithFormat:kImageName,reminderToDisplay.Name];
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *reminderString=[documentsDirectory stringByAppendingPathComponent:[NSString stringWithString:imageName]];
        self.reminderImage = [UIImage imageWithContentsOfFile:reminderString];
    }

    if (reminderImage != nil) 
    {
        UIImageView *imageView=[[UIImageView alloc] initWithFrame:CGRectMake(240, 3, 70, 63)];
        imageView.backgroundColor=[UIColor clearColor];
        [imageView setImage:reminderImage];
        cell.accessoryView = imageView;
        [imageView release];
    }
    else
    {
        UIImageView *imageView=[[UIImageView alloc] initWithFrame:CGRectMake(240, 3, 70, 63)];
        imageView.backgroundColor=[UIColor clearColor];
        UIImage *defaultImage = [UIImage imageNamed:kDefaultImage];
        [imageView setImage:defaultImage];
        cell.accessoryView = imageView;
        [imageView release];
    }
    cell.textLabel.text = reminderDetailsString;
        }
    return cell;
}
于 2013-03-29T11:25:39.847 回答