UITableViewCell(s) with default image overwritten with other images upon scrolling

两盒软妹~` 提交于 2019-11-29 15:48:21

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];

and

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

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

UPDATE:

-(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;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!