2013-10-02 91 views
0

我陷入了一個問題,我找不到解決方案,所以如果有人可以幫助我,我將不勝感激。我已閱讀了很多關於可能是問題的帖子,但我沒有找到任何解決方案。 我有一個.HreloadData不會調用cellForRowAtIndexPath

@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; 

} 
+0

numberOfRowsInSection:正在返回0或零然後 –

+0

你怎麼知道它沒有被調用?這是複製和粘貼?您還沒有在cellForRow中聲明'CellIdentifier' – Justin

+0

NSArray * jsonArray = [firstJsonData objectForKey:@「data」]; NSInteger numberOfRows = [jsonArray count]; NSLog(@「Count:%i」,numberOfRows);返回numberOfRows; –

回答

1

不要聲明伊娃UITableView *myTableView

不要@synthesize myTableView

總是通過屬性訪問self.myTableView

3

首先,你的代碼應該很好地運行。

  • 確定numberOfRowsInSection函數應該返回有效數字。
  • 確定「myTableView」不是零。

  • 確保「myTableView」被視爲背景黃色?

+0

閱讀評論@Mehmet,我知道它應該運行良好,我檢查並寫了關於'numberOfRowsInSection'的點我也寫了關於黃色科羅爾,所以接下來的兩點是荒謬的。有一個我忘記的IBOutlet,它將重新加載到它,而不是實際的表。 – Spire

+1

感謝這幫助了我,我甚至忘記了看段落中的行數,這對我來說顯然是個問題。 – StuartM

1
-(void)viewWillAppear:(BOOL)animated{ 

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

    [self.tableView reloadData]; not working 

} 

- (void)reloadTable { 

    [self.tableView reloadData]; 
} 

上面的代碼工作對我來說在選擇方法重載表訪問myTableView

相關問題