2013-03-29 82 views
3

糟糕,我面臨一個問題,沒有圖像的表格視圖單元格,即。該單元格默認圖像。滾動默認圖像消失,一些單元格的圖像出現在它上面。我已經實施了Mr.Rckoenes here的建議。然後我無法解決這個問題。這裏是我的實現代碼理解:UITableViewCell(s)與默認圖像覆蓋與其他圖像滾動

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

任何一個可以請幫我,在此先感謝:)

回答

3

剛剛成立dequeueReusableCellWithIdentifiernil,也reuseIdentifiernil像波紋管..

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil]; 

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

,並添加其他代碼在此if (cell == nil)如果條件..

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

不,沒有這種類型的問題,我面臨..它的公關當你的tableview向右滾動時,它會再次加載該圖像?並在上面的邏輯使用,否則我會上傳另一個邏輯,爲此... –

+0

你可以請繼續你說的話,上傳另一個邏輯,我的意思是我已經實施了你的建議,但仍然不工作我的方式 –