UITableViewCell(s) 默认图像在滚动时被其他图像覆盖

UITableViewCell(s) with default image overwritten with other images upon scrolling(UITableViewCell(s) 默认图像在滚动时被其他图像覆盖)

本文介绍了UITableViewCell(s) 默认图像在滚动时被其他图像覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

糟糕,我遇到了表格视图单元格没有图像的问题,即具有默认图像的单元格.滚动后默认图像消失,单元格中的一些图像出现在其上.我已经实施了 Mr.Rckoenes here .即使那样我也无法解决问题.这是我的实现代码以供理解:

Oops,I am facing an issue with table view cells having no image,i.e. the cell with default image.Upon scrolling the default image disappears and some image from a cell is appearing on it.I have implemented the suggestion from Mr.Rckoenes here .Even then I was unable to fix the issue.Here is my implementation code for understanding:

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

谁能帮帮我,提前谢谢:)

Can any one please help me,thanks in advance :)

推荐答案

只需将 dequeueReusableCellWithIdentifier 设置为 nil 并将 reuseIdentifier 设置为 nil 像下面这样..

just set dequeueReusableCellWithIdentifier to nil and also reuseIdentifier to nil like bellow..

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];

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

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

and also add your other code in this if (cell == nil) if condition..

更新:

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

这篇关于UITableViewCell(s) 默认图像在滚动时被其他图像覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:UITableViewCell(s) 默认图像在滚动时被其他图像覆盖

基础教程推荐