滚动和单击单元格时单元格内容消失

时间:2015-03-23 05:10:41

标签: objective-c iphone xcode ios8

滚动单元格内容消失。我正在使用此代码

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"SimpleCell";

    SimpleCell *cell = (SimpleCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    cell.name.text=@"hello";
    return cell;
}

我做错了什么?

1 个答案:

答案 0 :(得分:0)

如果您使用Nib作为自定义UITableViewCell,那么您不需要使用- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath方法从笔尖初始化它。

执行以下操作

viewDidLoad:方法中(如果您使用的是ViewController)或awakeFromNib:方法(如果您使用的是UIView)注册您的自定义单元格。

[tableView registerNib:[UINib nibWithNibName:@"SimpleCell"] bundle:[NSBundle mainBindle]] forCellReuseIdentifier:@"SimpleCell"];

现在在您的委托方法中,执行以下操作

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

    static NSString *simpleTableIdentifier = @"SimpleCell";
    SimpleCell *cell = (SimpleCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    cell.name.text=@"hello";
    return cell;
}