Ios UITableViewCell在重新加载数据后变为零

Ios UITableViewCell在重新加载数据后变为零,ios,objective-c,uitableview,cocoa-touch,Ios,Objective C,Uitableview,Cocoa Touch,我创建了一个名为tableView的UITableView。它是名为namesArray的数据数组 我有一个函数,它向数组中添加一个名称,如下所示: -(void)addName:(NSString*)name { [self.namesArray addObject: name]; [self.tableView reloadData]; } 在我调用tableView上的reloadData后,最后一个单元格(添加的单元格)没有显示在tableView上,numberOfR

我创建了一个名为
tableView
的UITableView。它是名为
namesArray
的数据数组

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

-(void)addName:(NSString*)name
{
    [self.namesArray addObject: name];
    [self.tableView reloadData];
}
在我调用
tableView
上的
reloadData
后,最后一个单元格(添加的单元格)没有显示在
tableView
上,
numberOfRowsInSection
返回实际的数字,以便为另一个单元格留出空间,但没有实际的单元格

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

cellforrowatinexpath
的代码:

-(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中返回的行数

- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section
应该是这样的:

[self.namesArray count];

首先是数据源并委托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。因为,当您调用此方法时,它会被重新加载,而CellForRowatineXpath每次都会被调用。因此,在数组完全填满之后,重新加载tableview

@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
{

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

我们可以看到
numberofrowsin部分中的代码吗?
这不是您应该让单元格出列的方式,您应该使用
dequeueReusableCellWithIdentifier(identifier:forindexath:)
,它总是返回一个有效的cell@AliRiahipour对我已经更新了post@DanielGalasko我不想为单元格注册nib,我想要默认的。您可以注册类或nib。请参阅API:
registerClass(:forReuseIdentifier)
更新了帖子,请查看[self.tableView reloadData]上的breackpoint设置;行并确保self.tableView不是nil。使断点检查它并删除插座,然后使用不同的tableView名称再次给出该函数在用户每次单击按钮时都被调用,因此它是动态的。。。
@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
{

IBOutlet UITextField *txtName;
IBOutlet UITableView *tableObject;
NSMutableArray *namesArray;
}
- (IBAction)btnAdd:(UIButton *)sender;
- (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];
}