下午好,
我想在我的TableViewController中使用“TouchesBegan”在一行内点击个人资料图片(如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
提前致谢。
答案 0 :(得分:0)
如果你的UIImageView应该像按钮一样,你应该把它变成UIButton而不是UIImageView。使用UIButton,您可以在Interface Builder中设置segue。这是UITableViewController的一个例子:
class TableViewController: UITableViewController, UITableViewDataSource, UITableViewDelegate {
let dataSource = NSMutableArray()
override func viewDidLoad() {
super.viewDidLoad()
dataSource.addObject(UIImage(named: "image.jpg")!)
dataSource.addObject(UIImage(named: "image.jpg")!)
tableView.reloadData()
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataSource.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("tableviewcell", forIndexPath: indexPath) as TableViewCell
let image = dataSource.objectAtIndex(indexPath.row) as UIImage
cell.button.setBackgroundImage(image, forState: UIControlState.Normal)
return cell
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "pushtodetail" {
println("performing segue from a button in a cell")
}
}
}