Ios UITableViewController自定义单元格在滚动和加载时消失并按其意愿显示

Ios UITableViewController自定义单元格在滚动和加载时消失并按其意愿显示,ios,objective-c,Ios,Objective C,我已经寻找答案两天多了。这似乎解决不了问题 这是手机的密码 - (myCustomCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ CustomData *m = (CustomData *)[self.allCustomData objectAtIndex:indexPath.row]; static NSString *CellIdentifi

我已经寻找答案两天多了。这似乎解决不了问题

这是手机的密码

- (myCustomCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    CustomData *m = (CustomData *)[self.allCustomData objectAtIndex:indexPath.row];

    static NSString *CellIdentifier = @"customCell";

    myCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    [cell setBackgroundColor:[UIColor colorWithRed:255.0f green:255.0f blue:255.0f alpha:1]];
    [cell.customTitle setText:m.name];
    [cell.customStatus setText:@"Download"];

    NSLog(@"%@",cell.customTitle.text);

    return cell;
}
“cell”从不返回零。日志始终打印正确的文本。但由于某种原因,这些细胞看起来是空的。加载时,我注意到第一个单元格显示为空,然后在滚动时,任意顺序的2-3个单元格随机变为空。第一个单元格经常出现在卷轴上

我似乎不明白这是什么问题


更新

这里有两个屏幕截图。第一个问题是如何加载表,我已经为调试目的添加了背景色。正如你看到的,第一个单元格没有出现

下面是第二个屏幕截图,我改变了方向并稍微滚动了一下。你可以看到第一个细胞神奇地出现,第二个细胞消失了

出于调试目的,我添加了这两种方法

- (void)tableView:(UITableView *)tableView willDisplayCell:(dtEditionCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{

NSLog(@"Display %@ %d %f",cell.editionTitle.text,indexPath.row,cell.frame.origin.x);


}
 - (void)tableView:(UITableView *)tableView didEndDisplayingCell:(dtEditionCell *)cell   forRowAtIndexPath:(NSIndexPath *)indexPath{

NSLog(@"End Display %@ %d %f",cell.editionTitle.text,indexPath.row,cell.frame.origin.x);
}

日志按预期打印。所有单元格都显示正确的文本。

我不知道它是否有什么作用,但您显示的方法与tableview使用的方法不完全相同。下面是我如何使用自定义单元格的。我做了很多,效果很好:

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


    TextCell *cell = [tableView dequeueReusableCellWithIdentifier:@"textCellId" forIndexPath:indexPath];

    TextFieldContent *cellContent=[[self.dataSource objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    cell.titleLabel.text=cellContent.titleLabel;
    cell.textField.placeholder=cellContent.placeHolder;
    cell.textField.tag=cellContent.tag;
    cell.textField.keyboardType=UIKeyboardTypeDecimalPad;
    cell.textField.delegate=self;

    return cell;

}

TextFieldContent是一个cutom对象,包含自定义单元格的所有属性。为了便于实现。

这种滚动行为是因为单元格在tableView中被重用。通过对每个单元格使用相同的标识符,您可以说“嘿,这些是相同的,请重新使用它们”。可能是您的CustomData对象为nil,这会导致名称显示为空。如果为nil,日志如何打印正确的输出?如果没有可重用的单元格,则可能由于您没有创建新的单元格而导致单元格出错。如果单元格为零,请确保已创建一个新单元格。尝试添加
if(cell==nil){cell=[[myCustomCell alloc]init];}
滚动Tableview数据源方法调用时,是否在xib/storyboard中设置了单元格标识符。因此,它会重新加载表数据。现在从您的问题来看,当表重新加载时,您的数组值也会发生变化。