【问题标题】:I am using a singleton class for web services and I want to call cellforrowindexpath in UITableView after JSON data arrived我正在为 Web 服务使用单例类,并且我想在 JSON 数据到达后在 UITableView 中调用 cellforrowindexpath
【发布时间】:2013-10-28 09:37:34
【问题描述】:

我正在使用单调类来获取 json 数据,我想在 tableview 中显示,但是当我第一次单击视图时数据不显示,第二次显示并且每次单击时单元格重复数据。

- (void)viewDidLoad
{
   [super viewDidLoad];
   singCls = [SingletoneClass sharedInstanceMethod];   // Declared Class for instance method of singletone class
   webserviceclas = [[WebserviceUtility alloc] init];
   [webserviceclas getorchardslist];
}
#pragma mark - TableView Delegates
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   return singCls.orcharsList.count;   // Get json data of orchards list in array
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   static NSString *CellIdentifier = @"Cell";

   UITableViewCell *orchardscell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

  if(orchardscell == nil)
  {

    orchardscell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  }
  orchardscell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  NSString *strcell = [singCls.orcharsList objectAtIndex:indexPath.row];
  orchardscell.textLabel.text = strcell;

  // Display Orchards list pass from json
  return orchardscell;
}

【问题讨论】:

  • 您忘记按TAB 几次。 (阅读:格式化您的代码。)

标签: ios objective-c json uitableview


【解决方案1】:

WebserviceUtility 类获取实际数据时未通知您的 tableview 控制器。我假设您的 WebserviceUtility 类调用 web 服务,一旦在此类中接收到数据,单例类的 orcharsList 数组将被更新。因此,如果到目前为止我是正确的,那么您需要在更新orcharsList 后通知 tableviewcontroller。并且在这里收到通知时,需要重新加载 tableView,然后调用您的 tableview 委托。

你需要做的是:

WebserviceUtility 类中添加协议方法。

@protocol WebServiceDelegate

-(void)fetchedData:(WebserviceUtility*) self;

@end

添加需要发送通知的委托属性

@interface WebserviceUtility {
    id<WebServiceDelegate>delegate;
}
@property (nonatomic, assign) id <WebServiceDelegate> delegate;

一旦数据可用就通知委托

if ([delegate respondsToSelector:@selector(fetchedData:)]) {
           [delegate fetchedData:self];
        } 

在你的 tableviewcontroller 类中实现这些协议,并将这个类作为委托添加到 WebserviceUtility

在.h文件中

@interface YourTableViewController: UITableViewController<WebServiceDelegate>

在 .m 文件中,将您的 tableviewcontroller 指定为WebserviceUtility 的委托

webserviceclas = [[WebserviceUtility alloc] init];
webserviceclas.delegate = self;

在你的控制器中实现协议方法

-(void)fetchedData:(WebserviceUtility*) self{
      //Reload your tableview here
}

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    我假设您没有嵌入一个回调来通知您的 UITableViewController 在您的单例完成加载数据时重新加载数据。现在第一次显示 tableView 时没有可用的数据,并且它不会自动刷新。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多