【发布时间】:2015-03-19 13:29:40
【问题描述】:
下午好,
我想使用“TouchesBegan”在 TableViewController 的一行中点击个人资料图像(如 Facebook 应用程序),以显示一个新的 ViewController,其中包含有关用户的更多信息。这意味着从 TableViewController(触摸 TableViewCell 的图像)然后转到(segue)另一个名为“ProfileViewController”的 ViewController。
当我尝试直接在我的 TableViewCell 中执行此操作时,它没有工作,因为它不是 ViewController(它是 TableViewController 的子类),因此我无法从中移至不同的 ViewController。
所以,我现在要做的是从我的 TableViewController 在 UIImage 中创建一个 TouchesBegan,但是我收到了一个警告(然后是崩溃),因为我使用 NSArray填写 UIImage URL,似乎我无法将 TouchesBegan 直接分配给图像。
你能帮我提供更多信息或一些例子吗?也许我缺少一些东西,因为这是我第一次尝试做这样的事情,任何帮助将不胜感激。
这是我的代码:
那是我的 TableViewController:
//
// CarTableViewController.m
// TableViewStory
//
#import "CarTableViewController.h"
#import "CarTableViewCell.h"
#import "CarTableViewController.h"
#import "CarDetailViewController.h"
#import <SDWebImage/UIImageView+WebCache.h>
@implementation CarTableViewController
@synthesize carMakes = _carMakes;
@synthesize carModels = _carModels;
@synthesize carImages = _carImages;
@synthesize likes = _likes;
@synthesize comments = _comments;
@synthesize username = _username;
@synthesize refuser = _refuser;
@synthesize profileImage = _profileImage;
- (void)viewDidLoad
{
[super viewDidLoad];
[self fetchJson];
[self.tableView reloadData];
// Initialize the refresh control.
self.refreshControl = [[UIRefreshControl alloc] init];
//self.refreshControl.backgroundColor = [UIColor blackColor];
//self.refreshControl.tintColor = [UIColor whiteColor];
[self.refreshControl addTarget:self
action:@selector(fetchJson)
forControlEvents:UIControlEventValueChanged];
}
- (void)viewWillAppear:(BOOL)animated
{
self.navigationController.navigationBar.hidden = YES;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [_jsonArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"carTableCell";
CarTableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[CarTableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
// Configure the cell...
cell.makeLabel.text = [[_jsonArray objectAtIndex:indexPath.row] valueForKey:@"id"];
cell.likes.text = [[_jsonArray objectAtIndex:indexPath.row] valueForKey:@"likes"];
cell.comments.text = [[_jsonArray objectAtIndex:indexPath.row] valueForKey:@"comments"];
cell.username.text = [[_jsonArray objectAtIndex:indexPath.row] valueForKey:@"username"];
cell.refuser.text = [[_jsonArray objectAtIndex:indexPath.row] valueForKey:@"user_ref"];
cell.modelLabel.text = [[_jsonArray objectAtIndex:indexPath.row] valueForKey:@"user"];
NSURL * imageURL = [NSURL URLWithString:[[_jsonArray objectAtIndex:indexPath.row] valueForKey:@"imagen"]];
[cell.carImage setImageWithURL:imageURL placeholderImage:[UIImage imageNamed:@"imagen"] options:SDWebImageRefreshCached];
NSURL * imageURL2 = [NSURL URLWithString:[[_jsonArray objectAtIndex:indexPath.row] valueForKey:@"image"]];
[cell.profileImage setImageWithURL:imageURL2
placeholderImage:[UIImage imageNamed:@"image"]
options:SDWebImageRefreshCached];
return cell;
}
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
CGPoint pt = [[touches anyObject] locationInView:_profileImage];
if (pt.x>=0 && pt.x<=100 && pt.y>=0 && pt.y<=100)
{
[self performSegueWithIdentifier:@"ID" sender:self];
}
else
{
NSLog(@"image not touched");
}
}
/*
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"ShowCarDetails"])
{
CarDetailViewController *detailViewController = [segue destinationViewController];
NSIndexPath *myIndexPath = [self.tableView indexPathForSelectedRow];
detailViewController.carDetailModel = [[NSArray alloc]
initWithObjects:
[[_jsonArray objectAtIndex:[myIndexPath row]] valueForKey:@"date"],
[[_jsonArray objectAtIndex:[myIndexPath row]] valueForKey:@"id"],
[[_jsonArray objectAtIndex:[myIndexPath row]] valueForKey:@"imagen"],
nil];
}
}
*/
-(void)fetchJson {
self.carModels = [[NSMutableArray alloc] init];
self.carMakes = [[NSMutableArray alloc] init];
self.carImages = [[NSMutableArray alloc] init];
self.likes = [[NSMutableArray alloc] init];
self.comments = [[NSMutableArray alloc] init];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
NSString * urlString = [NSString stringWithFormat:@"http://website.com/service.php"];
NSURL * url = [NSURL URLWithString:urlString];
NSData * data = [NSData dataWithContentsOfURL:url];
NSError *error;
[_jsonArray removeAllObjects];
_jsonArray = [NSJSONSerialization
JSONObjectWithData:data
options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves
error:&error];
for(int i=0;i<_jsonArray.count;i++)
{
NSDictionary * jsonObject = [_jsonArray objectAtIndex:i];
NSString* imagen = [jsonObject objectForKey:@"imagen"];
[_carImages addObject:imagen];
NSDictionary * jsonObject2 = [_jsonArray objectAtIndex:i];
NSString* user = [jsonObject2 objectForKey:@"user"];
[_carMakes addObject:user];
NSDictionary * jsonObject3 = [_jsonArray objectAtIndex:i];
NSString* date = [jsonObject3 objectForKey:@"date"];
[_carModels addObject:date];
}
NSLog(@"carModels ==> %@", _jsonArray);
dispatch_async(dispatch_get_main_queue(), ^{
{
[self.tableView reloadData];
[self.refreshControl endRefreshing];
}});
}
);
}
@end
这就是我的 TableViewController.h
//
// CarTableViewController.h
// TableViewStory
//
#import <UIKit/UIKit.h>
@interface CarTableViewController : UITableViewController
@property (nonatomic, strong) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *carImages;
@property (nonatomic, strong) NSMutableArray *carMakes;
@property (nonatomic, strong) NSMutableArray *carModels;
@property (nonatomic, strong) NSMutableArray *likes;
@property (nonatomic, strong) NSMutableArray *comments;
@property (nonatomic, strong) NSMutableArray *username;
@property (nonatomic, strong) NSMutableArray *refuser;
@property (nonatomic, strong) NSMutableArray *profileImage;
@property (nonatomic, strong) NSMutableArray *jsonArray;
@property (nonatomic, strong) IBOutlet UIImage *touchImageVIew;
@end
提前致谢。
【问题讨论】:
-
如果图像像一个按钮,你应该使用 UIButton 代替并设置背景图像。使用 UIButton 可以设置和执行 segue,无需使用 touchesBegan:: 并检查坐标。
-
谢谢@Thomas。如何在我的 UIImage 中放置一个按钮?
-
你误会了。您没有在 UIImageView 中放置按钮,而是将 UIImageView 替换为 UIButton。因此,不是将 UIImage 设置为 UIImageView 的源,而是设置 UIButton 的 backgroundImage 属性。我将通过示例发布答案。
标签: objective-c iphone uitableview uiimage tableviewcell