9

任何人都可以向我解释 MVC 在 UITableView 上是如何工作的,尤其是在从 Internet 获取数据时。

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

我编写了以下 ViewController 代码,该代码从 Internet 获取数据并使用 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
4

2 回答 2

10

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

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

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

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

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

更新
我正在添加一些代码示例以帮助澄清。首先,我想明确表示我不会提供完整的解决方案,因为这样做会要求我对必要的实际解决方案做出太多假设——而且因为有几种不同的方式可以与 AFNetworking 合作,网络服务,等等......而且我不想在那个兔子洞里被跟踪。(例如在客户端缓存数据、后台任务和 GCD 等...)只是向您展示如何连接基础知识——但您肯定想学习如何在后台任务上使用 AFNetworking,查看 Core Data或 NSCoding 用于缓存,以及一些其他主题来正确地做这类事情。

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

对于您的数据服务代码,我将创建一个“商店”类。(如果您还没有 Big Nerd Ranch iOS 书,请帮自己一个忙
。iOS Programming 第 4 版

对以下代码持保留态度 - 由于我无法进入的原因,我无法从我的 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。这种方法有利也有弊。
  • 我没有展示这个应用程序迫切需要的任何真正的异步行为或缓存
  • 这确实展示了如何完成一些模型职责并分离一些 tableview 数据源关注点。

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

于 2013-10-21T02:15:05.597 回答
1

就 UITableViewController 而言,通常所有角色模型、视图和控制器 (MVC) 都由您的 UITableViewController 本身扮演。您的代码也是如此。

  1. 作为模型 - 它为您的表格视图提供数据。
  2. 作为控制器 - 它控制表格的外观和感觉,如行数、部分、它们的高度和宽度等,从模型提供数据到表格视图
  3. As View - 它的 view 属性包含 UITableView

现在,要采用不同的方法,您可以将模型从控制器类中分离出来。为此,它有一个来自 NSObject 的子类,并让它设置可由 Controller 使用的状态。

希望这对你有意义。

于 2013-10-21T03:03:44.727 回答