2013-04-22 47 views
0

這個新的,只是試圖使一個鏈接加載,如果某個UITableView單元格被觸摸。如何在UITableViewCell觸摸上加載URL

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil]; 
    NSArray *deals = [dic objectForKey:@"deals"]; 
    NSDictionary *dealOne = [deals objectAtIndex:0]; 
    NSString *url = [dealOne objectForKey:@"url"]; 

    UITouch *touch = [[event allTouches] anyObject]; 

    if([touch isKindOfClass:[UITableViewCell class]]) 
    { 
    UITableViewCell *cell; 
    CGPoint location = [touch locationInView: self.view]; 
    cell.center = location; 

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]]; 
    } 
    NSLog(@"TABLECELL TOUCHED"); 
} 
+1

爲什麼你正在使用的touchesBegan,UITableView中已經有一個觸摸檢測委託是 - (空)的tableView:(UITableView的*)的tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath。在這個方法中,你可以簡單地調用[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]]; – Raj 2013-04-23 10:27:01

回答

0

使用以下方法求解。在這種情況下,當單元格被觸摸爲「finalDealOne」時,我想要鏈接加載。當單元格被雙擊時,鏈接加載。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil]; 
    NSArray *deals = [dic objectForKey:@"deals"]; 
    NSDictionary *dealOne = [deals objectAtIndex:0]; 
    NSString *title = [dealOne objectForKey:@"title"]; 
    NSString *finalDealOne = [NSString stringWithFormat:@"Deal One:\n\n''%@''\n\nDouble tap here to redeem offer.",title]; 

    (NSIndexPath *)indexPath 
    UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(finalDealOne)]; 
    tapped.numberOfTapsRequired = 2; 

    [cell addGestureRecognizer:tapped]; 
} 

爲了使這項工作,我包括以下參考:

- (void)finalDealOne 
{ 
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil]; 
    NSArray *deals = [dic objectForKey:@"deals"]; 
    NSDictionary *dealOne = [deals objectAtIndex:0]; 
    NSString *url = [dealOne objectForKey:@"url"]; 

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]]; 
}