细胞重复有问题

时间:2012-02-06 21:34:01

标签: iphone objective-c cocoa-touch uitableview

我有以下方法将单元格加载到我的表中。当我滚动时,正在重复带有文本“无结果”的单元格。我出错了怎么办?

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

1 个答案:

答案 0 :(得分:1)

有几件事:

  1. 如果您只想在单元格中添加文字标签,则无需向UILabel添加tableViewCell。只需使用单元格的textLabel属性或detailTextLabel属性(取决于您使用的tableviewcell类型)。

  2. 如果您确实要添加UILabel,则应将其添加到contentView的{​​{1}}媒体资源中。

  3. 当您向tableViewCell添加视图时,每次回收{{1}时,您都不应该tableViewcellalloc/init块之外if(!Cell) {}另一个tableViewcellUILabel'd。

  4. 根据您发布的代码和您看到的问题,看起来alloc/init为空。在哪里被填补?