1、表格视图设置
1)storyboard中需要新建TableViewController
2)实现DataSource委托相关
1 #pragma mark - Table view data source 2 3 4 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 5 { 6 // Return the number of rows in the section. 7 return [self.photos count]; 8 } 9 10 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 11 { 12 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Flickr Photo Cell" forIndexPath:indexPath]; 13 14 NSDictionary *photo = self.photos[indexPath.row]; 15 cell.textLabel.text = [photo valueForKeyPath:FLICKR_PHOTO_TITLE]; 16 cell.detailTextLabel.text = [photo valueForKeyPath:FLICKR_PHOTO_DESCRIPTION]; 17 18 return cell; 19 }
3)选中master记录更新detail
1 #pragma mark - UITableViewDelegate 2 3 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 4 { 5 id detail = self.splitViewController.viewControllers[1]; 6 if ([detail isKindOfClass:[UINavigationController class]]) { 7 detail = [((UINavigationController *)detail).viewControllers firstObject]; 8 if ([detail isKindOfClass:[ImageViewController class]]) { 9 [self prepareImageViewController:detail toDisplayPhoto:self.photos[indexPath.row]]; 10 } 11 } 12 }
4)Segue
1 #pragma mark - Navigation 2 3 -(void)prepareImageViewController:(ImageViewController *)ivc toDisplayPhoto:(NSDictionary *)photo 4 { 5 ivc.imageURL = [FlickrFetcher URLforPhoto:photo format:FlickrPhotoFormatLarge]; 6 ivc.title = [photo valueForKeyPath:FLICKR_PHOTO_TITLE]; 7 } 8 9 // In a storyboard-based application, you will often want to do a little preparation before navigation 10 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 11 { 12 // Get the new view controller using [segue destinationViewController]. 13 // Pass the selected object to the new view controller. 14 if ([sender isKindOfClass:[UITableViewCell class]]) { 15 NSIndexPath *indexPath = [self.tableView indexPathForCell:sender]; 16 if (indexPath) { 17 if ([segue.identifier isEqualToString:@"Display Photo"]) { 18 if ([segue.destinationViewController isKindOfClass:[ImageViewController class]]) { 19 [self prepareImageViewController:segue.destinationViewController toDisplayPhoto:self.photos[indexPath.row]]; 20 } 21 } 22 } 23 } 24 }
5)更新图片时ScrollView中需要同步刷新至初始状态,若上一幅进行了缩放或移位,需要恢复
- (void)setImage:(UIImage *)image { self.scrollView.zoomScale = 1.0; self.imageView.image = image; // does not change the frame of the UIImageView //[self.imageView sizeToFit]; // update the frame of the UIImageView self.imageView.frame = CGRectMake(0, 0, self.image.size.width, self.image.size.height); // self.scrollView could be nil on the next line if outlet-setting has not happened yet self.scrollView.contentSize = self.image ? self.image.size : CGSizeZero; [self.spinner stopAnimating]; }
2、多线程另一种方式
1 -(IBAction )fetchPhotos 2 { 3 [self.refreshControl beginRefreshing]; 4 NSURL *url = [FlickrFetcher URLforRecentGeoreferencedPhotos]; 5 dispatch_queue_t fetchQ = dispatch_queue_create("flickr fetcher", NULL); 6 dispatch_async(fetchQ, ^{ 7 NSData *results = [NSData dataWithContentsOfURL:url]; 8 NSDictionary *propertyListResults = [NSJSONSerialization JSONObjectWithData:results options:NSJSONReadingMutableContainers error:NULL]; 9 10 NSLog(@"Flickr Results = %@",propertyListResults); 11 12 NSArray *photos =[propertyListResults valueForKeyPath:FLICKR_RESULTS_PHOTOS]; 13 dispatch_async(dispatch_get_main_queue(), ^{ 14 [self.refreshControl endRefreshing]; 15 self.photos = photos; 16 }); 17 }); 18 19 }