UITableViewCells笔尖在我滚动时显示默认值

时间:2013-02-14 21:41:37

标签: ios uitableview xib nib

我已经创建了一个UITableViewCell笔尖,我正在加载到我的tableview中,但是当我滚动表格时发生了一些奇怪的事情,如果我向下滚动进入的新单元格会显示默认值一瞬间然后如果我向上滚动,同样的事情发生在顶部。

下面的代码决定根据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;
}

任何帮助将不胜感激

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

您应该检查您的UITableViewCell *单元格对象是否为零(即,是否有任何要出列的内容)。如果是,请分配一个新的。