【问题标题】:reloadData wont call cellForRowAtIndexPathreloadData 不会调用 cellForRowAtIndexPath
【发布时间】:2013-10-02 13:15:02
【问题描述】:

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

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

还有一个.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;

}

【问题讨论】:

  • numberOfRowsInSection: 返回 0 或 nil
  • 你怎么知道它没有被调用?这是复制粘贴吗?你还没有在 cellForRow 中声明 CellIdentifier
  • NSArray* jsonArray = [firstJsonData objectForKey:@"data"]; NSInteger numberOfRows = [jsonArray 计数]; NSLog(@"Count: %i", numberOfRows);返回 numberOfRows;
  • 不调用 cellforrowatindexpath 的唯一原因通常有两个原因,numberofrows 返回 0 或 tableview 在 cellforrow 和 numberofrows 之间的时间范围内以某种方式被释放
  • numberOfRowsInSection 中尝试记录tableViewmyTableView 的值,以验证它们是否相同。

标签: iphone ios xcode uitableview reloaddata


【解决方案1】:

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

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

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

【讨论】:

  • 阅读 cmets @Mehmet,我知道它应该运行良好,我检查并写了关于 numberOfRowsInSection 的观点我还写了关于黄色的文章,所以接下来的两点很荒谬。有一个我已经忘记的 IBOutlet,它将重新加载到它,而不是实际的表。
  • 感谢这对我有帮助,我什至忘了查看部分中的行数,这对我来说显然是个问题。
【解决方案2】:

不要声明 ivar UITableView *myTableView

不要@synthesize myTableView

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

【讨论】:

    【解决方案3】:
    -(void)viewWillAppear:(BOOL)animated{
    
        [self performSelector:@selector(reloadTable) withObject:nil afterDelay:0.2];
    
        [self.tableView reloadData]; not working 
    
    }
    
    - (void)reloadTable {
    
        [self.tableView reloadData];
    }
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-23
      • 1970-01-01
      相关资源
      最近更新 更多