Jon
Jon

Reputation: 4732

Having trouble with cells repeating

I have the following method that loads cells into my table. The cell with text "No Results" is being repeated though as I scroll. What am I doing wrong with the dequeueing?

    - (UITableViewCell *)tableView:(UITableView *)iTableView cellForRowAtIndexPath:(NSIndexPath *)iIndexPath {
    static NSString *kCellID = @"cellID";

    UITableViewCell *aCell = [iTableView dequeueReusableCellWithIdentifier:kCellID];
    if (aCell == nil) {
        aCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:kCellID] autorelease];
    }

    if (iTableView == self.searchDisplayController.searchResultsTableView) {
        if (self.isSearching) {
            static NSString *aProgressIdentifier = @"SearchProgress";
            aCell = [iTableView dequeueReusableCellWithIdentifier:aProgressIdentifier];
            if (!aCell) {
                aCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:aProgressIdentifier] autorelease];
            }

            aCell.userInteractionEnabled = NO;
            UILabel *aLabel = [[UILabel alloc] initWithFrame:CGRectZero];
            aLabel.text = @"Searching...";
            aLabel.font = [UIFont systemFontOfSize:16.0f];
            aLabel.textColor = [UIColor grayColor];
            aLabel.textAlignment = UITextAlignmentCenter;
            [aLabel sizeToFit];
            aLabel.frame = CGRectMake(floorf((self.view.frame.size.width / 2.0f) - (aLabel.frame.size.width / 2.0f)), floorf((aCell.frame.size.height / 2.0f) - (aLabel.frame.size.height / 2.0f)), aLabel.frame.size.width, aLabel.frame.size.height);
            [aCell addSubview:aLabel];
            [aLabel release];

            UIActivityIndicatorView *aSpinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
            [aCell addSubview:aSpinner];
            aSpinner.frame = CGRectMake(floorf((aCell.frame.size.width / 2.0f) - (aSpinner.frame.size.width / 2.0f)) - floorf(aLabel.frame.size.width / 2.0f) - 20.0f, floorf((aCell.frame.size.height / 2.0f) - (aSpinner.frame.size.height / 2.0f)), aSpinner.frame.size.width, aSpinner.frame.size.height);
            [aSpinner startAnimating];
            [aSpinner release];
        } else {
            Class anObject = [self.masterArray objectAtIndex:iIndexPath.section];
            if ([anObject isKindOfClass: [NSArray class]]) {
                NSArray *sectionArray = [self.masterArray objectAtIndex:iIndexPath.section];
                if (sectionArray.count > 0) {
                    id aCellObject = [sectionArray objectAtIndex:iIndexPath.row];
                    if ([aCellObject isKindOfClass:[CMACustomer class]]) {
                        CMACustomer *aCustomerObject = aCellObject;
                        aCell.textLabel.
                        text = aCustomerObject.customerFullName;
                        aCell.detailTextLabel.text = nil;                
                    } else if ([aCellObject isKindOfClass:[CMAReservation class]]) {
                        CMAReservation *aReservationObject = aCellObject;
                        aCell.textLabel.text = aReservationObject.fullName;
                        aCell.detailTextLabel.text = aReservationObject.orderDescription;     
//                        aCell.detailTextLabel.text = aReservationObject.reservationTimeAndDate;                
                    } 
                } else {
                    aCell.userInteractionEnabled = NO;
                    UILabel *aLabel = [[UILabel alloc] initWithFrame:CGRectZero];
                    aLabel.text = @"No Results.";
                    aLabel.font = [UIFont systemFontOfSize:16.0f];
                    aLabel.textColor = [UIColor grayColor];
                    aLabel.textAlignment = UITextAlignmentCenter;
                    [aLabel sizeToFit];
                    aLabel.frame = CGRectMake(floorf((self.view.frame.size.width / 2.0f) - (aLabel.frame.size.width / 2.0f)), floorf((aCell.frame.size.height / 2.0f) - (aLabel.frame.size.height / 2.0f)), aLabel.frame.size.width, aLabel.frame.size.height);
                    [aCell addSubview:aLabel];
                    [aLabel release];
                }
            }
        }
    } else {
        NSString *aLocalizedTitle = [[self.defaultScopeArray objectAtIndex:iIndexPath.row] objectForKey:@"localizedTitle"];
        NSString *aDefaultTitle = [[self.defaultScopeArray objectAtIndex:iIndexPath.row] objectForKey:@"defaultTitle"];
        aCell.textLabel.text = CMALocalizedStringWithDefaultValue(aLocalizedTitle, aDefaultTitle);

        if ([[[self.currentScopeArray objectAtIndex:iIndexPath.row] objectForKey:@"enabledByDefault"] boolValue]) {
            aCell.accessoryType = UITableViewCellAccessoryCheckmark;
        } else {
            aCell.accessoryType = UITableViewCellAccessoryNone;
        }
    }
    return aCell;
}

Upvotes: 0

Views: 102

Answers (1)

Justin
Justin

Reputation: 464

A couple of things:

  1. If you just want text labels in your cells there is no need to add a UILabel to the tableViewCell. Just use the textLabel property of the cell or the detailTextLabel property (depending on the tableviewcell type you're using).

  2. If you do want to add a UILabel you should add it to the contentView property of the tableViewCell.

  3. When you are adding views to a tableViewcell, you really shouldn't alloc/init it outside of the if(!Cell) {} block as every time you're recycling that tableViewcell, another another UILabel is alloc/init'd.

  4. According to the code you posted and the problem you are seeing, it looks like sectionArrayis empty. Where is that being filled?

Upvotes: 1

Related Questions