Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone UITableView重用问题_Iphone_Xcode - Fatal编程技术网

Iphone UITableView重用问题

Iphone UITableView重用问题,iphone,xcode,Iphone,Xcode,这是我的cellForRowAtIndexPath代码: -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *CellIdentifier = @"MonthViewPopUpCell"; UITableViewCell *cell = [tableView dequeueReusableCe

这是我的cellForRowAtIndexPath代码:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"MonthViewPopUpCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        NSArray *nibObjcet = [[NSBundle mainBundle] loadNibNamed:@"MonthViewPopUpCell" owner:self options:nil];
        cell = [nibObjcet objectAtIndex:0];

        cellTitle.font = [UIFont fontWithName:fontB size:size4];
        cellSubtitle.font = [UIFont fontWithName:fontR size:size4];
    }

    if (indexPath.row == 0) {
        cellTitle.text = @"11:03";
    }

    return cell;
}
由于某种原因,我不明白,tableview错误地重用了我的自定义单元格。我原以为这段代码会让第一个单元格显示11:03,其他所有单元格显示10:00,就像在xib文件中一样,但其他一些单元格也显示11:03,当我疯狂地上下滚动时,它们的位置正在改变

有人能告诉我我做错了什么吗


感谢

它不会在同一位置重复使用同一单元格,它只是将已存储在内存中的单元格重新用于另一位置的表,因此每次都需要设置该单元格的文本,例如:

    if (cell == nil) {
        NSArray *nibObjcet = [[NSBundle mainBundle] loadNibNamed:@"MonthViewPopUpCell" owner:self options:nil];
        cell = [nibObjcet objectAtIndex:0];

        cellTitle.font = [UIFont fontWithName:fontB size:size4];
        cellSubtitle.font = [UIFont fontWithName:fontR size:size4];
    }

    if (indexPath.row == 0) {
        cellTitle.text = @"11:03";
    }
    else{
        cellTitle.text = @"10:00";
    }

通常,一旦单元移出UITableView的可见区域,它就会被重用。你可以做一件事。还要在nib中检查自定义单元格的可重用标识符是否设置为 蒙特维普普赛尔

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"MonthViewPopUpCell"; ////As you have specified in XIB

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        NSArray *nibObjcet = [[NSBundle mainBundle] loadNibNamed:@"MonthViewPopUpCell" owner:self options:nil];
        cell = [nibObjcet objectAtIndex:0];

        cellTitle.font = [UIFont fontWithName:fontB size:size4];
        cellSubtitle.font = [UIFont fontWithName:fontR size:size4];
    }

    if (indexPath.row == 0) {
        cellTitle.text = @"11:03";
    }else {
        cellTitle.text = @"10:00";
   }


    return cell;
}

下面是调用自定义单元格的好方法。我希望它能起作用

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

MonthViewPopUpCell *cell = (MonthViewPopUpCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    NSArray *nibObjcet = [[NSBundle mainBundle] loadNibNamed:@"MonthViewPopUpCell" owner:self options:nil];
    cell = [nibObjcet objectAtIndex:0];

    cellTitle.font = [UIFont fontWithName:fontB size:size4];
    cellSubtitle.font = [UIFont fontWithName:fontR size:size4];
}

if (indexPath.row == 0) {
    cellTitle.text = @"11:03";
}else{

    cellTitle.text = @"10:00";

}

return cell;
}

但你们在哪里初始化单元呢?我试过了,但它说未声明标识符的用户“单元”。。。如果我添加此行:UITableViewCell*单元格;我把它放在你代码的第一行上面,它工作了…很抱歉我不接受,但它不允许我,因为我的重复次数少于15分,只需使用cellTitle.text=@10.00为if条件添加一个else部分@用户2328703,检查我的答案。我只为自定义单元格使用一个额外的xib文件。不为此使用类在您的情况下,单元格处于弱势地位。像这样尝试UITableViewCell cell=UITableViewCell[tableView dequeueReusableCellWithIdentifier:CellIdentifier];或者创建单独的UITableViewCell类。我建议您创建一个方法-voidPrepareforUseCell:UITableViewCell*cell;您是否检查了xib并将自定义单元格的可重用标识符设置为代码中使用的标识符。