【问题标题】:UITableViewCell become nil after reloadDataUITableViewCell 在 reloadData 后变为 nil
【发布时间】:2015-12-21 12:26:32
【问题描述】:

我是一个名为tableView 的 UITableView。这是一个名为namesArray的数据数组。

我有一个向数组添加名称的函数,如下所示:

-(void)addName:(NSString*)name
{
    [self.namesArray addObject: name];
    [self.tableView reloadData];
}

在我在tableView 上调用reloadData 后,最后一个单元格(添加的那个)没有显示在tableView 上,numberOfRowsInSection 返回实际数字,因此另一个单元格有空间但有不是真正的细胞。

我正在调试cellForRowAtIndexPath,我发现当cellForRowAtIndexPath 调用新单元格时,dequeueReusableCellWithIdentifier 在调用其他单元格时返回 nil(当然 indexPath.row == 0 除外)它返回一个单元格。

cellForRowAtIndexPath的代码:

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

    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }

    cell.textLabel.text=[self.namesArray objectAtIndex:indexPath.row];

    return cell;
}

行数:

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.namesArray.count;
}

注意:如果我尝试使用NSLog 打印namesArray 的最后一个对象,它看起来很好(最后一个对象是创建的新对象)所以重新加载tableView 的数据会出现问题

你能帮帮我吗?谢谢!

【问题讨论】:

  • 我们可以看到numberOfRowsInSection中的代码吗?
  • 这不是你应该如何让单元格出队的方式,你应该使用dequeueReusableCellWithIdentifier(identifier:forIndexPath:),它总是返回一个有效的单元格
  • @AliRiahipour 是的!我已经更新了帖子
  • @DanielGalasko 我不想为单元格注册笔尖,我想要默认的。
  • 您可以注册一个类或一个NIB。查看 API:registerClass(:forReuseIdentifier)

标签: ios objective-c uitableview cocoa-touch


【解决方案1】:

检查您在 numberOfRowsInSection 中返回的行数

- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section

应该是这样的:

[self.namesArray count];

【讨论】:

  • 更新了帖子,请看一下
  • 在 [self.tableView reloadData] 上设置断点;行并确保 self.tableView 不为零。
【解决方案2】:

首先 dataSource 并委托 tableview。之后确保完美使用以下方法

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
     }

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return [your array count];
    }


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

            UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
            if (!cell) {
                cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
            }

            cell.textLabel.text=[self.namesArray objectAtIndex:indexPath.row];

            return cell;
        }

之后用

重新加载tableView
[_tableViewName reloadData];

【讨论】:

  • 做断点检查兄弟并删除插座并再次使用不同的tableview名称
【解决方案3】:
-(void)addName:(NSString*)name
{
    [self.namesArray addObject: name];
    [self.tableView reloadData];
}

如果你一次又一次地调用这个函数来填充数组,那么不要在这个方法中重新加载 tableview。因为,它会超时重新加载,您调用此方法,并且每次都会调用 cellForRowAtIndexPath。所以,在你的数组完全填满之后,重新加载 tableview

【讨论】:

  • 每次用户点击按钮时都会调用该函数,所以它是动态的......
【解决方案4】:

.h

@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
{

IBOutlet UITextField *txtName;
IBOutlet UITableView *tableObject;
NSMutableArray *namesArray;
}
- (IBAction)btnAdd:(UIButton *)sender;

.m

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    namesArray = [[NSMutableArray alloc] init];
}




- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
    return namesArray.count;
}


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

    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }

    cell.textLabel.text=[namesArray objectAtIndex:indexPath.row];

    return cell;
}

-(void)addName:(NSString*)name
{
    [namesArray addObject: name];
    [tableObject reloadData];
}
- (IBAction)btnAdd:(UIButton *)sender {
    [self addName:txtName.text];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-02
    • 1970-01-01
    • 1970-01-01
    • 2011-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多