![](/img/trans.png)
[英]How to change `UIImageView` in custom cell in `UITableView`
[英]How to pass a UIImageView from UITableView (custom cell) to DetailViewController?
我要把這根頭發撕掉。 我正在嘗試將圖像URL從我的TableViewController
傳遞到DetailViewController
( FullArticleViewController
),以便可以設置UIImageView
,但我嘗試的任何方法似乎都沒有用。 請參閱下面的代碼:
MyTableViewController.h
@interface MyTableViewController : UIViewController <UISearchBarDelegate>{
IBOutlet UITableView *DoctorsTableView;
NSArray *Doctors;
NSMutableData *data;
NSArray *searchResults;
}
@property (strong, nonatomic) NSString *cellImageLink;
@property (strong, nonatomic) UINavigationBar *navigationBar;
MyTableViewController.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *DoctorsTableIdentifier = @"DoctorsCell";
DoctorsCell *cell = (DoctorsCell *)[tableView dequeueReusableCellWithIdentifier:DoctorsTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"DoctorsCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
if (tableView == self.searchDisplayController.searchResultsTableView) {
NSLog(@"Using the search results");
cell.firstnameLabel.text = [[searchResults objectAtIndex:indexPath.row] objectForKey:@"node_title"];
cell.descriptionLabel.text = [[searchResults objectAtIndex:indexPath.row] objectForKey:@"Opening Paragraph"];
NSString *firstLink = [[NSString alloc] init];
firstLink = [[[searchResults objectAtIndex:indexPath.row] objectForKey:@"Image"] objectForKey:@"filename"];
NSString *secondLink = [[NSString alloc] init];
secondLink = [NSString stringWithFormat:@"URL HERE%@",firstLink];
NSLog(@"second link is %@", secondLink);
cellImageLink = secondLink;
[cell.featureImage sd_setImageWithURL:[NSURL URLWithString:secondLink]];
} else {
NSLog(@"Using the Full List!");
cell.firstnameLabel.text = [[Doctors objectAtIndex:indexPath.row] objectForKey:@"node_title"];
cell.descriptionLabel.text = [[Doctors objectAtIndex:indexPath.row] objectForKey:@"Opening Paragraph"];
NSString *firstLink = [[NSString alloc] init];
firstLink = [[[Doctors objectAtIndex:indexPath.row] objectForKey:@"Image"] objectForKey:@"filename"];
NSString *secondLink = [[NSString alloc] init];
secondLink = [NSString stringWithFormat:@"URL HERE%@",firstLink];
NSLog(@"second link is %@", secondLink);
cellImageLink = secondLink;
[cell.featureImage sd_setImageWithURL:[NSURL URLWithString:secondLink]];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
FullArticleViewController *detailViewController = [[FullArticleViewController alloc]
initWithNibName:@"FullArticleViewController" bundle:nil];
if ([searchResults count]) {
detailViewController.title = [[searchResults objectAtIndex:indexPath.row] objectForKey:@"node_title"];
detailViewController.articleDetail = [searchResults objectAtIndex:indexPath.row];
} else {
detailViewController.title = [[Doctors objectAtIndex:indexPath.row] objectForKey:@"node_title"];
detailViewController.articleDetail = [Doctors objectAtIndex:indexPath.row];
NSLog(@"%@", Doctors);
}
FullArticleViewController *viewController = [[FullArticleViewController alloc]
initWithNibName:@"DetailViewController"
bundle:nil];
viewController.featureImage = searchResults[indexPath.row][@"Image"][@"filename"];
[self.navigationController pushViewController:detailViewController animated:YES];
}
FullArticleViewController.h (詳細視圖)
@interface FullArticleViewController : UIViewController
{
IBOutlet UIScrollView *scroller;
IBOutlet UILabel *firstnameLabel;
IBOutlet UILabel *descriptionLabel;
IBOutlet UILabel *bodyLabel;
}
@property (nonatomic, copy) NSDictionary *articleDetail;
@property (strong, nonatomic) IBOutlet UIImageView *featureImage;
-(IBAction)goBack:(id)sender;
FullArticleViewController.m (詳細視圖)
#import "SDWebImage/UIImageView+WebCache.h"
#import "FullArticleViewController.h"
#import "DoctorsCell.h"
#import "MyTableViewController.h"
@interface FullArticleViewController ()
@end
@implementation FullArticleViewController
@synthesize articleDetail;
@synthesize featureImage;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
featureImage = [[UIImageView alloc] init];
[scroller setScrollEnabled:YES];
[scroller setContentSize:CGSizeMake(320, 5000)];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
firstnameLabel.text = [articleDetail objectForKey:@"node_title"];
descriptionLabel.text = [articleDetail objectForKey:@"Opening Paragraph"];
bodyLabel.text = [articleDetail objectForKey:@"Body"];
}
MyTableViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
FullArticleViewController *detailViewController = [[FullArticleViewController alloc]
initWithNibName:@"FullArticleViewController" bundle:nil];
if ([searchResults count]) {
detailViewController.title = [[searchResults objectAtIndex:indexPath.row] objectForKey:@"node_title"];
detailViewController.articleDetail = [searchResults objectAtIndex:indexPath.row];
detailViewController.cellImageLink = searchResults[indexPath.row][@"Image"][@"filename"];
} else {
detailViewController.title = [[Doctors objectAtIndex:indexPath.row] objectForKey:@"node_title"];
detailViewController.articleDetail = [Doctors objectAtIndex:indexPath.row];
detailViewController.cellImageLink = Doctors[indexPath.row][@"Image"][@"filename"];
}
NSLog(@"cellImageLink = %@", detailViewController.cellImageLink);
[self.navigationController pushViewController:detailViewController animated:YES];
}
FullArticleViewController.m
在viewDidLoad
設置圖像
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
NSLog(@"cellImageLink = %@", self.cellImageLink);
featureImage = [[UIImageView alloc] init];
//maybe set the frame here?
[self.featureImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://domainnamehere.com/%@",cellImageLink]]];
[scroller setScrollEnabled:YES];
[scroller setContentSize:CGSizeMake(320, 5000)];
firstnameLabel.text = [articleDetail objectForKey:@"node_title"];
descriptionLabel.text = [articleDetail objectForKey:@"Opening Paragraph"];
bodyLabel.text = [articleDetail objectForKey:@"Body"];
}
通常,在推送新視圖控制器之前,您將在選擇單元格時設置詳細視圖屬性。 這樣的事情可以做到:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
FullArticleViewController *detailView = [[FullArticleViewController alloc] init];
detailView.articleDetail = [searchResults objectAtIndex:indexPath.row];
[self.navigationController pushViewController detailView animated:YES];
}
注意,我正在為視圖控制器使用默認的初始化程序,並且還假設您正在使用UINavigationController
。 您可能需要更改其中一項或兩項以適合您的代碼。
然后,您將要使用相同的SDWebImage
類別在FullArticleViewController.m
viewDidLoad
中設置圖像URL。 它可能看起來像這樣:
- (void)viewDidLoad
{
[scroller setScrollEnabled:YES];
[scroller setContentSize:CGSizeMake(320, 5000)];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
firstnameLabel.text = [articleDetail objectForKey:@"node_title"];
descriptionLabel.text = [articleDetail objectForKey:@"Opening Paragraph"];
bodyLabel.text = [articleDetail objectForKey:@"Body"];
[self.featureImage sd_setImageWithURL::[NSURL URLWithString:[[self.articleDetail objectForKey:@"Image"] objectForKey:@"filename"]];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.