当从互联网获取表的数据时,有人可以用 UItableview 来解释 MVC 吗?

2023-11-27

任何人都可以向我解释 MVC 在 UITableView 方面如何工作,特别是从互联网获取数据时。

我很想知道 UItableview 的模型、视图和控制器是什么

我编写了以下 ViewController 代码,该代码从互联网获取数据并使用 AFNetworking 框架将其显示在表格上。

您能告诉我如何更改它并将其分离为模型、视图和控制器吗? 我还编写了一个刷新类,我猜它是模型的一部分。您能告诉我具体如何进行更改并将其成为模型的一部分吗?

编辑:下面的答案可以帮助我从理论上理解这个概念,有人可以帮助我相应地更改代码吗(通过编写一个关于如何将数组调用到此类并填充表的新类,因为我正在使用 json 解析器)。我想实施它。而不仅仅是理论上理解它。

#import "ViewController.h"

#import "AFNetworking.h"

@implementation ViewController

@synthesize tableView = _tableView, activityIndicatorView = _activityIndicatorView, movies = _movies;

- (void)viewDidLoad {
    [super viewDidLoad];

    // Setting Up Table View
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.bounds.size.width, self.view.bounds.size.height) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    self.tableView.hidden = YES;
    [self.view addSubview:self.tableView];

    // Setting Up Activity Indicator View
    self.activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    self.activityIndicatorView.hidesWhenStopped = YES;
    self.activityIndicatorView.center = self.view.center;
    [self.view addSubview:self.activityIndicatorView];
    [self.activityIndicatorView startAnimating];

    // Initializing Data Source
    self.movies = [[NSArray alloc] init];

    NSURL *url = [[NSURL alloc] initWithString:@"http://itunes.apple.com/search?term=rocky&country=us&entity=movie"];
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];

    UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
    [refreshControl addTarget:self action:@selector(refresh:) forControlEvents:UIControlEventValueChanged];
    [self.tableView addSubview:refreshControl];
    [refreshControl endRefreshing];

    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
        self.movies = [JSON objectForKey:@"results"];
        [self.activityIndicatorView stopAnimating];
        [self.tableView setHidden:NO];
        [self.tableView reloadData];

    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
        NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo);
    }];

    [operation start];
}

- (void)refresh:(UIRefreshControl *)sender
{
    NSURL *url = [[NSURL alloc] initWithString:@"http://itunes.apple.com/search?term=rambo&country=us&entity=movie"];
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];


    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
        self.movies = [JSON objectForKey:@"results"];
        [self.activityIndicatorView stopAnimating];
        [self.tableView setHidden:NO];
        [self.tableView reloadData];

    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
        NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo);
    }];

    [operation start];
    [sender endRefreshing];

}

- (void)viewDidUnload {
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}

// Table View Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.movies && self.movies.count) {
        return self.movies.count;
    } else {
        return 0;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellID = @"Cell Identifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
    }

    NSDictionary *movie = [self.movies objectAtIndex:indexPath.row];
    cell.textLabel.text = [movie objectForKey:@"trackName"];
    cell.detailTextLabel.text = [movie objectForKey:@"artistName"];

    NSURL *url = [[NSURL alloc] initWithString:[movie objectForKey:@"artworkUrl100"]];
    [cell.imageView setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeholder"]];

    return cell;
}

@end

你问的是一个很大的问题。但让我尽可能简单地回答。

  • 模型 - 您的数据源;最终是您的网络服务数据
  • 控制器应该是拥有表视图并协调视图上的设置属性并对视图中的事件做出反应并根据需要对模型进行更改的东西
  • 视图——表格视图和表格视图单元格的组合

有很多方法可以协调您的 Web 数据和表格视图,但我建议的一种方法是将您的 Web 服务调用重构为一个单独的商店类 - 比如说 iTunesStore - 让该类负责对服务进行调用并使用结果设置内部数组,它还应该能够返回行计数以及给定行索引的特定项目。

然后,您可以让此类响应对所需表视图委托方法的调用。其他需要考虑的事情,使另一个类成为单例,让它符合 UITableviewDatasource 协议本身并将其分配为表视图的数据源。

就像我说的,这是一个大问题,给你很多选择,但我已经给了你一些关于下一步该去哪里的考虑事项。

UPDATE
我添加了一些代码示例来帮助澄清。首先,我想明确表示,我不会提供完整的解决方案,因为这样做需要我对必要的实际解决方案做出太多假设——而且因为有几种不同的方法可以使用 AFNetworking 、网络服务等等……我不想陷入困境。 (例如在客户端缓存数据、后台任务和 GCD 等...)只是向您展示如何连接基础知识 - 但您肯定想学习如何在后台任务上使用 AFNetworking,请查看 Core Data或用于缓存的 NSCoding,以及其他一些正确执行此类操作的主题。

可以说,在适当的解决方案中:
- 您不想同步调用您的网络服务
- 您也不希望每次都重新请求相同的数据 - 即不要从服务重新下载相同的记录,除非它发生了变化
- 我不会在这里展示如何做这些事情,因为它超出了范围;查看下面的书籍推荐以及此链接以了解有关这些主题的想法Ray Wenderlich - 将核心数据与 Web 服务同步

对于您的数据服务代码,我将创建一个“store”类。 (帮自己一个忙,如果你还没有《Big Nerd Ranch》iOS 书,就去买吧。
iOS编程第四版

对以下代码持保留态度 - 由于我无法进入的原因,我无法从我的 Mac(在 Win 机器上)执行此操作,而且我也无法复制或什至通过电子邮件给自己发送代码。 ..所以我在 StackOverflow 编辑器中完成所有操作...

我的 iTunesStore 合同(头文件)如下所示:

// iTunesStore.h
#import <Foundation/Foundation.h>

@interface iTunesStore : NSObject

- (NSUInteger)recordCount;
- (NSDictionary*)recordAtIndex:(NSUInteger)index; // could be a more specialized record class

+ (instancetype)sharedStore; // singleton

@end

...并且实现看起来像这样:

// iTunesStore.m
#import "iTunesStore.h"

// class extension
@interface iTunesStore()

@property (nonatomic, strong) NSArray* records;
@end

@implementation iTunesStore
-(id)init
{
    self = [super init];
    if(self) {
       // DO NOT DO IT THIS WAY IN PRODUCTION
       // ONLY FOR DIDACTIC PURPOSES - Read my other comments above
       [self loadRecords];
    }

   return self;
}
- (NSUInteger)recordCount
{
    return [self.records count];
}
- (NSDictionary*)recordAtIndex:(NSUInteger)index
{
    NSDictionary* record = self.records[index];
}
-(void)loadRecords
{
   // simulate loading records from service synchronously (ouch!)
   // in production this should use GCD or NSOperationQue to
   // load records asynchrononusly
   NSInteger recordCount = 10;
   NSMutableArray* tempRecords = [NSMutableArray arrayWithCapacity:recordCount];  

   // load some dummy records
   for(NSInteger index = 0; index < recordCount; index++) {
      NSDictionary* record = @{@"id": @(index), @"title":[NSString stringWithFormat:@"record %d", index]};
      [tempRecords addObject:record];
   }
   self.records = [tempRecords copy];
}
// create singleton instance
+ (instancetype)sharedStore
{
    static dispatch_once_t onceToken;
    static id _instance;
    dispatch_once(&onceToken, ^{
       _instance = [[[self class] alloc] init];
    });

    return _instance;
}

@end

我现在有一个“存储”对象单例,我可以用它来获取记录、返回给定记录并告诉我记录计数。现在我可以从视图控制器中移动很多执行此操作的逻辑。

现在我不需要在你的 VC viewDidLoad 方法中执行此操作。理想情况下,您的存储对象中应该有一个异步方法来获取记录,并有一个块在加载记录后回调您。在块内您重新加载记录。类似“可能”的签名看起来像:

[[iTunesStore sharedStore] loadRecordsWithCompletion:^(NSError* error){
   ... if no error assume load records succeeded
   ... ensure we are on the correct thread
   [self.tableView reloadData]; // will cause table to reload cells
}];

您的视图控制器数据源方法现在如下所示:

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection(NSInteger)section {
      [[iTunesStore sharedStore] recordCount];
  }

在 cellForRowAtIndexPath 内部 - 我还调用我的存储对象来获取正确的记录

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

  // ... get cell
  // ... get record
  NSDictionary* record = [[iTunesStore sharedStore] recordAtIndex:indexPath.row];

  // ... configure cell]
  return cell;
  }

这就是它的要点。如上所述,其他要做的事情是:

  • 让 iTunesStore 实现 UITableViewDataSource,然后直接处理 tableview 数据源方法 - 如果您这样做,您不想使 iTunesStore 成为单例。并且您可以将 iTunesStore 的实例设置为 tableview 的委托,而不是 viewcontroller。这种方法有利有弊。
  • 我还没有显示出这个应用程序所急需的任何真正的异步行为或缓存
  • 这确实展示了如何完成一些模型职责并分离一些表视图数据源问题。

希望这将有助于为您提供一些关于您可能探索的不同方向的想法。 快乐编码!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

当从互联网获取表的数据时,有人可以用 UItableview 来解释 MVC 吗? 的相关文章

随机推荐