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

后端 未结 1 1325
南旧
南旧 2020-12-21 08:54

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 ap

相关标签:
1条回答
  • 2020-12-21 09:46

    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;
    }
    
    0 讨论(0)
提交回复
热议问题