xcode reloadData 不会调用 cellForRowAtIndexPath

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19137797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 03:59:12  来源:igfitidea点击:

reloadData wont call cellForRowAtIndexPath

iphoneiosxcodeuitableviewreloaddata

提问by Spire

I got myself in a problem that I cant find a solution to, so if someone can help me I would appreciate it. I've read a lot of posts on what could be the problem but I did not found any solution. I have a .h

我遇到了一个无法解决的问题,所以如果有人可以帮助我,我将不胜感激。我已经阅读了很多关于可能是什么问题的帖子,但我没有找到任何解决方案。我有一个 .h

@interface FirstViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>{
    UITableView *myTableView;
}
@property (strong, nonatomic)  UITableView *myTableView;
@end

and a .m

和一个 .m

@interface FirstViewController ()

@end

@implementation FirstViewController

@synthesize myTableView;
static NSString *CellIdentifier = @"CellIdentifier";

- (void)viewDidLoad
{
    [super viewDidLoad];
    myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 460) style:UITableViewStylePlain];
    myTableView.dataSource = self;
    myTableView.delegate = self;

    [self.view addSubview:myTableView];
    myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    myTableView.rowHeight = 325;
    myTableView.backgroundColor = [UIColor yellowColor];
    [myTableView setDelegate:self];

    [myTableView setDataSource:self];
    [myTableView registerClass:[MTTableViewCell class] forCellReuseIdentifier:CellIdentifier];
    [self separateByType];

}
-(void)separateByType
{
  NSURL *url = [NSURL URLWithString:tempUrl];
            ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
            [request setDelegate:self];
            [request startAsynchronous];
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
    NSString *response = [request responseString];
    SBJsonParser *jsonReader = [SBJsonParser new];

    firstJsonData = [NSMutableDictionary dictionary];
    firstJsonData = [jsonReader objectWithString:response error:nil];

    NSLog(@"Calling reloadData on %@", self.myTableView);

    [self.myTableView reloadData];

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

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

    return [[firstJsonData objectForKey:@"data"] count];
}

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


    MTTableViewCell *cell = (MTTableViewCell *)[myTableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    if (cell == nil) {
        cell = [[MTTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    [cell.titleTextlabel setText:[JSONDATA objectAtIndex:[indexPath row]]];
    [cell.descriptionTextlabel setText:[JSONDATA objectAtIndex:[indexPath row]]];
    return cell;

}

回答by Mehmet AKYOL

First Of All , Your code should run well.

首先,您的代码应该运行良好。

  • Be Sure that numberOfRowsInSection function should return valid number.
  • Be Sure that "myTableView" is not nil.

  • Be sure that "myTableView" is seen as background yellow?

  • 确保 numberOfRowsInSection 函数应该返回有效数字。
  • 确保“myTableView”不为零。

  • 确定“myTableView”被视为背景黄色?

回答by Mike Pollard

Don't declare ivar UITableView *myTableView

不要声明 ivar UITableView *myTableView

Don't @synthesize myTableView

@synthesize myTableView

Always access myTableViewvia the property accessor self.myTableView

始终myTableView通过属性访问器访问self.myTableView

回答by Umar Farooq

-(void)viewWillAppear:(BOOL)animated{

    [self performSelector:@selector(reloadTable) withObject:nil afterDelay:0.2];

    [self.tableView reloadData]; not working 

}

- (void)reloadTable {

    [self.tableView reloadData];
}

above code works for me reload table in selector method.

上面的代码适用于我在选择器方法中重新加载表。