ios - UITableViewCells Nib 在滚动时显示默认值

标签 ios uitableview xib nib

我创建了一个 UITableViewCell Nib ,将其加载到我的表格 View 中,但是当我滚动表格时,如果我向下滚动,出现的新单元格会瞬间显示默认值,然后会发生一些奇怪的事情如果我向上滚动,顶部也会发生同样的事情。

下面的代码根据 myObj 中可用的数据来决定加载哪个 Nib ,希望这是可以理解的

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (indexPath.section == 0) {
        // Set cell height
        [self tableView:tableView heightForRowAtIndexPath:indexPath];

        //call obj array with all of the values from my sorting algorithum
        SearchResultItem *myObj = (searchResultItem*)[dataArrayOfObjects objectAtIndex:indexPath.row];



        // This gets the year string ready  ----->

        // Create Year string
        NSString *yearRangeString = [[NSString alloc] init];

        // do some special stuff here with the years
        if ((myObj.yearStart < 1) && (myObj.yearFinish < 1)){
            //            yearRangeLabel.text = @"";
            yearRangeString = @"";
        }
        if ((myObj.yearStart < 1) && (myObj.yearFinish > 1)){
            //            yearRangeLabel.text = [NSString stringWithFormat:@"upto %i", myObj.yearFinish];
            yearRangeString = [NSString stringWithFormat:@"upto %i", myObj.yearFinish];
        }
        if ((myObj.yearStart > 1) && (myObj.yearFinish < 1)){
            //            yearRangeLabel.text = [NSString stringWithFormat:@"from %i", myObj.yearStart];
            yearRangeString = [NSString stringWithFormat:@"from %i", myObj.yearStart];
        }
        if ((myObj.yearStart > 1) && (myObj.yearFinish > 1)){
            //            yearRangeLabel.text = [NSString stringWithFormat:@"%i to %i",myObj.yearStart, myObj.yearFinish];
            yearRangeString = [NSString stringWithFormat:@"%i to %i",myObj.yearStart, myObj.yearFinish];
        }

        // adjust year string is (submodel) is avalible or not
        if (([yearSelectedString isEqualToString:@"all"]) && ([sModelSelectedString isEqualToString:@"all"])) {
            yearRangeString = [NSString stringWithFormat:@"(%@) %@", myObj.subModel, yearRangeString];
            yearRangeLabel.text = yearRangeString;
        }
        else if ((![yearSelectedString isEqualToString:@"all"]) && ([sModelSelectedString isEqualToString:@"aSearchResultItemll"])) {
            yearRangeString = [NSString stringWithFormat:@"(%@)", myObj.subModel];
            yearRangeLabel.text = yearRangeString;
        }
        else if (([yearSelectedString isEqualToString:@"all"]) && (![sModelSelectedString isEqualToString:@"all"])) {
            yearRangeString = [NSString stringWithFormat:@"%@", yearRangeString];
            yearRangeLabel.text = yearRangeString;
        }
        else if ((![yearSelectedString isEqualToString:@"all"]) && (![sModelSelectedString isEqualToString:@"all"])) {
            yearRangeLabel.text = nil;
        }

    // check year string if too large use B nibs ----->

        // Get yearRangeString width to see if you need to use a yearRangeLabel with second line
        CGSize yearStringSize = [yearRangeString sizeWithFont:[UIFont fontWithName:@"Helvetica" size:15] constrainedToSize:CGSizeMake(226, 21) lineBreakMode:NSLineBreakByWordWrapping ];

        // NSLog(@"width = %f, height = %f", yearStringSize.width, yearStringSize.height);



     // Get rest of nibs ready ----->

        // Set custom cell to display
        // check to see if there is a description, if there is choose the correct tableviewcell

        if (([myObj.note isEqualToString:@""]) && (([sModelSelectedString isEqualToString:@"all"]) || ([yearSelectedString isEqualToString:@"all"])) && (yearStringSize.width <= 100.00)) {

            // Configure the cell using custom cell
            [[NSBundle mainBundle] loadNibNamed:@"auSearchCell" owner:self options:nil];

            // Set up the different labels in each cell
            descriptionLabel.text = myObj.sDescription;
            iDLabel.text = [NSString stringWithFormat:@"%i", myObj.mID];
            cLabel.text = myObj.cDesc;
            INLabel.text = [NSString stringWithFormat:@"%i", myObj.ssID];

            cell = automativeSearchCell;
        }

        /// other if statments for different types of cells

         //Disclosure Indicator
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    return cell;
}

任何帮助将不胜感激

最佳答案

看来您遇到了一些问题。您不应该调用 heightForRowAtIndexPath:,它是由 TableView 调用的。您应该根据索引路径设置所需的任何高度。它看起来也像是每次都将普通的 UITableViewCell 出列,然后如果字符串太长,则切换到其他单元格。您应该首先检查字符串大小,然后将正确的单元格出队。另外,看起来您每次都有太长的字符串时都会加载 Nib 。相反,您应该只注册您的 Nib (也许在 viewDidLoad 中),然后使用 dequeueReusableCellWithIdentifier:forIndexPath: 方法而不是您正在使用的旧方法。该方法始终保证使单元出列,因此不需要 if cell == nil 子句。

关于ios - UITableViewCells Nib 在滚动时显示默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14884566/

相关文章:

ios - 自定义 UITableViewCell 图层截断

ios - 删除所有以前的 UITableViewCell Swift 3/4 iOS

iphone - 如何像 XCode 3 一样查看 XCode 4 中的对象层次结构?

ios - Xcode 7.1 中 xib 文件的内部错误

ios - 让@IBDesignable UIView 在不崩溃 Interface Builder 的情况下进行绘制

ios - LaunchScreen.storyboard 如何重定向到 Main.storyboard

objective-c - identifierForVendor 和 iOS6

ios - 更改 cellForItemAtIndexPath 中的图像 :

ios - 单击推送通知时正确重定向到选项卡栏导航 Controller 内的 View Controller

ios - 如何从表格 View 行单元格中删除单独的行?