![](/img/trans.png)
[英]How to set image from main viewController to tableView cell in objective C?
[英]objective c TableView cell Detail ViewController
我的viewDidLoad
- (void)viewDidLoad {
[super viewDidLoad];
appdataModel = [AppDataModel getInstance];
appdataModel.newsApiUrl = homePagesUrl;
self.view.backgroundColor = [UIColor whiteColor];
NSString *path = [[NSBundle mainBundle] pathForResource:@"contacts" ofType:@"plist"];
contactsArray = [NSArray arrayWithContentsOfFile :path];
[self GetHomePageData];
/**** for left side menu ***/
SWRevealViewController *revealViewController = self.revealViewController;
if ( revealViewController )
{
[self.sideBarButton setTarget: self.revealViewController];
[self.sideBarButton setAction: @selector( revealToggle: )];
[self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
}
}
我從這種方法獲取數據:
-(void)GetHomePageData{
// url_to_load = homePagesNews;
NSString *urlString = [NSString stringWithFormat:@"%@",newsApiUrl];
NSURL *url = [[NSURL alloc]initWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLResponse *response;
NSData *GETReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
res = [NSJSONSerialization JSONObjectWithData:GETReply options:NSJSONReadingMutableLeaves|| NSJSONReadingMutableContainers error:nil];
}
用於UITableView中的顯示數據
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier =@"Cell";
CustomCell *customCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
details = res[indexPath.row];
NSString *titleNews = details[@"title"];
NSString *newsDateTime = details[@"datetime"];
NSString *NewsImageName = details[@"featured_image"];
//UIImage *image = [UIImage imageNamed:NewsImageName ];
// UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:NewsImageName]]];
customCell.customFirstNameLabel.text = titleNews;
customCell.customLastnameLabel.text = newsDateTime;
//customCell.customImageView.image =image;
NSURL *imageUrl=[details valueForKey:@"featured_image"];
[customCell.customImageView sd_setImageWithURL:imageUrl placeholderImage:[UIImage imageNamed:@"no_image.png"]];
//[tableView reloadData];
return customCell;
}
在選擇單元格后在表格視圖中顯示detils數據
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:@"detailSegue"]) {
//s NSIndexPath *path = Nil;
NSString *titleString = Nil;
NSIndexPath *path = [self.tableView indexPathForSelectedRow];
titleString = [res objectAtIndex:path.row];
[[segue destinationViewController] setTitle:titleString];
}
}
但是現在正在工作。 可以幫我解決這個問題..謝謝
NSString *NewsImageName = details[@"featured_image"];
,它是字典而不是字符串!,您可以刪除該行。
[details valueForKey:@"featured_image"]
它是字符串,但是您直接將其分配給NSURL
。
NSURL *imageUrl=[details valueForKey:@"featured_image"];
如下替換
NSURL* imageUrl= [NSURL URLWithString:[details valueForKey:@"featured_image"]];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.