2016-02-29 65 views
1

我有一种情况,必须在我的UITableView中插入单元格。 我实现了这三个代表功能,当数据在viewDidLoad中定义时,它运行良好。无法在UITableView中插入单元格来自API的数据

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    categoryData = [NSMutableArray arrayWithObjects:@"Egg Benedict", @"Mushroom Risotto", @"Full Breakfast", @"Hamburger", @"Ham and Egg Sandwich", @"Creme Brelee", @"White Chocolate Donut", @"Starbucks Coffee",@"Ham and Cheese Panini", nil]; 
    _categoryMenuTable.dataSource = self; 
    _categoryMenuTable.delegate = self; 
} 

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
    return [categoryData count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 
    cell.textLabel.text = [NSString stringWithFormat:[categoryData objectAtIndex:indexPath.row]]; 
    return cell; 
} 

但在我的情况下categoryData来自一个API异步,viewDidLoad后明显加载。

所以真正的问题是,在后面的情况下,我无法调用委托方法。

PS我也试过

[_categoryMenuTable reloadData]; 

-(void)setCategories:(NSMutableArray*)arr { 
    categoryData = arr; 
    _categoryMenuTable.dataSource = self; 
    _categoryMenuTable.delegate = self; 
    [_categoryMenuTable reloadData]; 
} 

数据设置为categoryData后,我还有一点我想提的是,我使用MMDawerController https://github.com/mutualmobile/MMDrawerController

我怎样才能得到从API的数据到我的表?

+0

如何解析来自服务器的数据? – shpasta

+0

我解析它并将其存储在一个NSMutableArray * arr –

+0

你可以展示你是如何做这件事的,即数据解析。 – shpasta

回答

2

有许多障碍解决问题,但总算熬过来的。让我一个一个解释问题和解决方案。

问题1: 最初我无法调用不同控制器的功能,但过了一段时间后,我可以像这样调用它。

ClassName* cm = [self.storyboard instantiateViewControllerWithIdentifier:@"identifierName"]; 
[cm functionName : argumentName]; 

问题2: 现在我能够调用的功能,也可以发送数据,但仍然无法触发后,一些搜索的UITableView的委托方法,我知道了,我要重新加载数据触发委托像这样的方法。

[UITableViewObject reloadData]; 

问题3: 委托方法仍然是不叫,所以我去的viewDidAppear方法,现在我可以打电话委托方法。但无法提供API的数据来viewDidAppear。因此,我创建了一个全局类,并将来自API(在A类中)的数据设置为它,并从此全局类中再次获取数据(在B类中),并且一切正常。

我提到的代码使用它可能对某人有所帮助。

//default.m (data load in this class from API) 

GlobalData *obj = [GlobalData getInstance]; 
// categoryDataFromApi is of type NSMutableArray and set it to a property of global class 
obj. categoryData = categoryDataFromApi; 



// category.m (UITableView exist in this class and all the delegate methods also exist in this class) 
// reloading data in viewDidAppear to trigger delegate methods. 

- (void)viewDidAppear:(BOOL)animated { 
    GlobalData *obj = [GlobalData getInstance]; 
    categoryData = obj.categoryData; 
    [_categoryMenuTable reloadData]; 
} 



// Global.h 

#import <UIKit/UIKit.h> 
@interface GlobalData 
    @property(nonatomic, strong) NSMutableArray *categoryData; 
    +(GlobalData*)getInstance; 
@end 


// Global.m 

#import "GlobalData.h" 
@implementation GlobalData 
    static GlobalData *instance = nil; 
    +(GlobalData *)getInstance{ 
     @synchronized(self){ 
      if(instance == nil) 
       instance = [GlobalData new]; 
     } 
     return instance; 
    } 
@end 

请让我知道是否存在一些更好的解决方案。

谢谢

0

使用reloadData代替reload调用UITableView委托方法

+0

哎呀!我不好,我已经更新了我使用的代码。 –

0

如果你的数据分析是正确的(如你在评论中提到的),其原因可能是,你是从后台线程更新UI。应该只从主线程更新UI。试试这一个,当你设置的类别对象和更新的tableView:

dispatch_async(dispatch_get_main_queue(), ^{ 
    [self setCategories:categoriesArray]; 
}); 
+0

我从不同的控制器调用了setCategories方法,因为控制器A中的数据获取负载,控制器B中存在表视图,所以我必须从控制器A调用setCategories并将数据发送到控制器B的setCategory方法。 –

+0

你可以从任何你想要的地方打电话给你的二传手。我在谈论线程。如果你不是从主线程更新tableView,它将无法工作。在'setCategories'中放置断点,并在XCode Navigator(左侧面板)中查看它所调用的线程号。 – shpasta

+0

这可能是问题,因为我在数据加载后调用了这个函数。让我检查,并会回复给你。 –

相关问题