UITableView 无法正确重新加载
UITableView not reloading correctly
我经常遇到这个问题,其中 table 视图要么没有第一次加载单元格,要么显示临时标签。在这两种情况下,当 table 第二次加载时(通过另一个按钮强制它重新加载或通过转到应用程序中的上一个视图并返回到 table),一切都会显示就像它应该的那样。
我使用 [table reloadData]
在 viewDidAppear:
方法中重新加载 table,因为它在 viewDidAppear:
方法中不起作用,在另一种情况下我将其放入一个按钮操作。
我也在使用这种方法来设置我放置在情节提要中的单元格的标签:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"Cell";
UITableViewCell *cell = [contactsTableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
UILabel *label = (UILabel *)[cell viewWithTag:1001];
label.text = [Array3 objectAtIndex:indexPath.row];
return cell;
}
编辑
我在实现下面声明数组,如下所示:
NSMutableArray * Array3;
之后,我在 ViewDidLoad 方法中声明了数组的其余部分:
Array3 = [NSMutableArray arrayWithObjects: nil];
然后我通过在 ViewDidLoad 方法中调用一个函数来加载数组中的元素,以用元素填充数组。
编辑 2
显示更多我如何填充数组
- (void)viewDidLoad {
[super viewDidLoad];
Array3 = [NSMutableArray arrayWithObjects: nil];
[self loadArray];
}
- (void) loadArray
//populate array in here.
// I use the add object method
// [Array3 addobject:object];
{
我认为Array3
第一次加载时是nil。
先来个好消息:你的方法cellForRowAtIndexPath
是正确的。
在 -viewDidAppear:
和 -viewWillAppear
中都不需要调用 reloadData
。如果您在视图被覆盖时修改 Array3 的内容,则此评论可能不成立,这是一种特殊情况。
这不是在哪里定义方法的问题,而是在哪里调用它的问题。填充 Array3 内容的合理位置是
required init(coder aDecoder: NSCoder!)
或
override func viewDidLoad()
陷阱
此外,您需要所有委托方法(numberOfSectionsInTableView
、numberOfRowsInSection
、cellForRowAtIndexPath
)根据您的数组返回一致的值。一个简单的技术是使用数组中的条目数来驱动 numberOfRowsInSection
.
什么时候使用reloadData?
reloadData
的典型用法如下所示:
- (void)loadArray {
//populate array in here.
...
[self.tableview reloadData]; // Possibly postpone to main thread
}
给小区标识符唯一的例如你可以
NSString *cellIdentifier =[NSString stringWithFormate:@"%d",indexPath.row];
也许这会对你有所帮助...
我相信在 Array3
的初始设置中,您必须将此变量分配到内存,然后在不同的函数中使用,例如 tableView:cellForIndexPath:
因此,您将使用 Array3 = [[NSMutableArray alloc] init];
而不是 Array3 = [NSMutableArray arrayWithObjects: nil];
由于您在这行代码中将其保留为空数组,因此您不会使用 [[NSMutableArray alloc] initWithObjects: nil];
,因为它实际上是相同的交易
我经常遇到这个问题,其中 table 视图要么没有第一次加载单元格,要么显示临时标签。在这两种情况下,当 table 第二次加载时(通过另一个按钮强制它重新加载或通过转到应用程序中的上一个视图并返回到 table),一切都会显示就像它应该的那样。
我使用 [table reloadData]
在 viewDidAppear:
方法中重新加载 table,因为它在 viewDidAppear:
方法中不起作用,在另一种情况下我将其放入一个按钮操作。
我也在使用这种方法来设置我放置在情节提要中的单元格的标签:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"Cell";
UITableViewCell *cell = [contactsTableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
UILabel *label = (UILabel *)[cell viewWithTag:1001];
label.text = [Array3 objectAtIndex:indexPath.row];
return cell;
}
编辑
我在实现下面声明数组,如下所示: NSMutableArray * Array3; 之后,我在 ViewDidLoad 方法中声明了数组的其余部分: Array3 = [NSMutableArray arrayWithObjects: nil]; 然后我通过在 ViewDidLoad 方法中调用一个函数来加载数组中的元素,以用元素填充数组。
编辑 2 显示更多我如何填充数组 - (void)viewDidLoad {
[super viewDidLoad];
Array3 = [NSMutableArray arrayWithObjects: nil];
[self loadArray];
}
- (void) loadArray
//populate array in here.
// I use the add object method
// [Array3 addobject:object];
{
我认为Array3
第一次加载时是nil。
先来个好消息:你的方法
cellForRowAtIndexPath
是正确的。在
-viewDidAppear:
和-viewWillAppear
中都不需要调用reloadData
。如果您在视图被覆盖时修改 Array3 的内容,则此评论可能不成立,这是一种特殊情况。
这不是在哪里定义方法的问题,而是在哪里调用它的问题。填充 Array3 内容的合理位置是
required init(coder aDecoder: NSCoder!)
或
override func viewDidLoad()
陷阱
此外,您需要所有委托方法(numberOfSectionsInTableView
、numberOfRowsInSection
、cellForRowAtIndexPath
)根据您的数组返回一致的值。一个简单的技术是使用数组中的条目数来驱动 numberOfRowsInSection
.
什么时候使用reloadData?
reloadData
的典型用法如下所示:
- (void)loadArray {
//populate array in here.
...
[self.tableview reloadData]; // Possibly postpone to main thread
}
给小区标识符唯一的例如你可以
NSString *cellIdentifier =[NSString stringWithFormate:@"%d",indexPath.row];
也许这会对你有所帮助...
我相信在 Array3
的初始设置中,您必须将此变量分配到内存,然后在不同的函数中使用,例如 tableView:cellForIndexPath:
因此,您将使用 Array3 = [[NSMutableArray alloc] init];
Array3 = [NSMutableArray arrayWithObjects: nil];
由于您在这行代码中将其保留为空数组,因此您不会使用 [[NSMutableArray alloc] initWithObjects: nil];
,因为它实际上是相同的交易