iOS-只有部分单元格在UITableView中加载,而其余单元格不加载';在滚动浏览后才会显示
我有一个从Parse下载数据的应用程序iOS-只有部分单元格在UITableView中加载,而其余单元格不加载';在滚动浏览后才会显示,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我有一个从Parse下载数据的应用程序 -(void)viewWillAppear:(BOOL)animated{ [self getData]; } 在getData方法中,下载所有数据后,它在主线程上调用self.tableView reloadData: dispatch_async(dispatch_get_main_queue(), ^{ [self.tableView reloadData]; }); 但只有第一个单元
-(void)viewWillAppear:(BOOL)animated{
[self getData];
}
在getData方法中,下载所有数据后,它在主线程上调用self.tableView reloadData:
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
但只有第一个单元格更新正确,其余单元格保留其占位符值,直到我滚动浏览它们为止
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0){
static NSString *CellIdentifier = @"Cell1";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
UIButton *signUp = (UIButton *)[cell.contentView viewWithTag:3];
signUp.clipsToBounds = YES;
signUp.layer.cornerRadius = 8;
[signUp addTarget:self
action:@selector(toSignUp) forControlEvents:UIControlEventTouchDown];
[signUp setHidden:NO];
UILabel *spotsOutOf500 = (UILabel *)[cell.contentView viewWithTag:1];
spotsOutOf500.text = [NSString stringWithFormat:@"%d/%d",onSpot,ofSpots];
[spotsOutOf500 setHidden:NO];
UILabel *spotsLeft = (UILabel *)[cell.contentView viewWithTag:2];
spotsLeft.text = [NSString stringWithFormat:@"%d spots left",ofSpots - onSpot];
if (ofSpots - onSpot < 0){
spotsLeft.text = @"0 spots left";
}
[spotsLeft setHidden:NO];
return cell;
}
if (indexPath.row == 1){
static NSString *CellIdentifier = @"Cell2";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
UIButton *getRobin = (UIButton *)[cell.contentView viewWithTag:102];
getRobin.clipsToBounds = YES;
getRobin.layer.cornerRadius = 4;
[getRobin addTarget:self
action:@selector(getRobin) forControlEvents:UIControlEventTouchDown];
UIButton *sendToFriend = (UIButton *)[cell.contentView viewWithTag:103];
sendToFriend.clipsToBounds = YES;
sendToFriend.layer.cornerRadius = 4;
[sendToFriend addTarget:self
action:@selector(sendToFriend) forControlEvents:UIControlEventTouchDown];
return cell;
}
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
PFObject *stock = stocks[indexPath.row-2];
// int row = indexPath.row;
// NSLog (@"%@,%d",stock,row);
UILabel *ticker = (UILabel *)[cell.contentView viewWithTag:1001];
ticker.text = stock[@"ticker"];
UILabel *dateAndTime = (UILabel *)[cell.contentView viewWithTag:1002];
dateAndTime.text = stock[@"dateAndTime"];
return cell;
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
if(indexPath.row==0){
静态NSString*CellIdentifier=@“Cell1”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
如果(单元格==nil){
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:CellIdentifier];
}
UIButton*注册=(UIButton*)[cell.contentView视图带标记:3];
signUp.clipstobunds=是;
signUp.layer.cornerRadius=8;
[注册地址:self
操作:@选择器(toSignUp)用于控制事件:UIControlEventTouchDown];
[注册设置隐藏:否];
UILabel*spotsOutOf500=(UILabel*)[cell.contentView视图带标记:1];
spotsOutOf500.text=[NSString stringWithFormat:@“%d/%d”,onSpot,ofspot];
[spotsOutOf500 setHidden:否];
UILabel*spotsLeft=(UILabel*)[cell.contentView视图带标记:2];
spotsLeft.text=[NSString stringWithFormat:@“%d个点左”,of spots-onSpot];
如果(OFSPOT-onSpot<0){
spotsLeft.text=@“左0点”;
}
[spotsLeft setHidden:否];
返回单元;
}
if(indexath.row==1){
静态NSString*CellIdentifier=@“Cell2”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
如果(单元格==nil){
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:CellIdentifier];
}
UIButton*getRobin=(UIButton*)[cell.contentView视图带标记:102];
getRobin.clipstobunds=YES;
getRobin.layer.cornerRadius=4;
[getRobin addTarget:self
操作:@selector(getRobin)forControlEvents:UIControlEventTouchDown];
UIButton*sendToFriend=(UIButton*)[cell.contentView视图带标记:103];
sendToFriend.clipstobunds=是;
sendToFriend.layer.cornerRadius=4;
[sendToFriend添加目标:自我
操作:@selector(sendToFriend)for controlEvents:UIControlEventTouchDown];
返回单元;
}
静态NSString*CellIdentifier=@“Cell”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
如果(单元格==nil){
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:CellIdentifier];
}
PFObject*stock=stocks[indexPath.row-2];
//int row=indexPath.row;
//NSLog(@“%@,%d”,库存,行);
UILabel*ticker=(UILabel*)[cell.contentView视图带标记:1001];
ticker.text=股票[@“ticker”];
UILabel*日期和时间=(UILabel*)[cell.contentView视图带标记:1002];
dateAndTime.text=股票[@“dateAndTime”];
返回单元;
}
这件事我已经坚持了4天了。谢谢你的帮助
编辑这是getData方法:
PFQuery *query = [PFQuery queryWithClassName:@"Stock"];
[query orderByDescending:@"createdAtEST"];
[query setLimit:25];
[query whereKey:@"live" equalTo:@"true"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
stocks = [NSMutableArray arrayWithArray:objects];
for (int i = 0; i < stocks.count; i++){
if (![stocks[i][@"sold"] boolValue]){
[stocks removeObjectAtIndex:0];
i--;
}
else{
break;
}
}
for (int i = 0; i < stocks.count; i++){
NSLog(@"%@",stocks[i][@"ticker"]);
}
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
}];
PFQuery*query=[pfqueryqueryquerywithclassname:@“Stock”];
[查询orderByDescending:@“createdAtEST”];
[查询设置限制:25];
[query whereKey:@“live”equalTo:@“true”];
[查询findObjectsInBackgroundWithBlock:^(NSArray*对象,NSError*错误){
stocks=[NSMutableArray arrayWithArray:objects];
对于(int i=0;i
尝试调用[self-getData]代码>输入
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
[self getData];
}
还有,我会像这样修理你的手机
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier1 = @"Cell1";
static NSString *CellIdentifier2 = @"Cell2";
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell;
if (indexPath.row == 0){
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1];
}
// Do your set up
}
else if (indexPath.row == 1) {
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2];
}
// Do your set up
}
else {
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Get data from your datasource
// Do your set up
}
return cell;
}
这是Swift语法,但我相信同样的属性也是可用的
在tableview上,使用属性
tableview.beginupdates()
和tableview.endUpdates()
这将动态更新tableview,而无需滚动。最好在将新单元格输入到tableview本身时添加这些单元格。希望这有帮助。getData中的最后一行是[self.tableView reloadData],整个函数都发生在后线程中。
所以,我假设您正试图从两个线程操作表视图,这可能导致某种锁。
我将从getData中删除该行或在主线程中执行该行。我在调试脚本时也将脚本布局设置为Any x Any。我可以看到传递到每个单元格的数据是正确的。也许我应该试着重新画细胞?我已尝试在cellForRowAtIndexPath中显示cell setNeedDisplay,但没有任何结果。在设置要重新加载的数据之前,您正在调用reloadData
。设置数据是什么意思?我该怎么做?也许你应该发布你的getData
方法。我看不出您是如何存储数据的。添加到问题中。谢谢您使用您的方法,只有可见的股票被成功加载。但是,当我向下滚动时,所有单元格都是空的。只有在上下滚动之后,所有数据才会显示。@LeonardoMurri您是否尝试过在viewDidLoad
或ViewDidDisplay
中调用getData
?这没有任何作用,,,,没有任何意义。为什么它在滚动之前和之后工作。我不确定,但它与自动布局有关