2017-06-20 110 views
0

我想在單元格單擊時打開特定的URL。打開表格視圖單元格的URL點擊

我想使用對象來做到這一點,但它不起作用。字符串'url'獲取(null)。

代碼:

//setting object 
[self.url2 addObject:@"http://google.nl"]; 

//method to select cell 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Get your url, eg: NSString *url = yourArray[indexPath.row]; 
    NSString *url = [NSString stringWithFormat:@"%@", [self.url2 objectAtIndex:indexPath.row]]; 
    [[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]]; 
} 

這甚至可能嗎?

謝謝。

+0

爲什麼不是NSString * url = [self.url2 objectAtIndex:indexPath.row]; ? –

+0

返回「訪問不良」的原因 - 錯誤 – Anoniem

+0

您可以發佈所有代碼嗎? –

回答

1

試試這個片斷,

NSMutableArray *URLArray = [[NSMutableArray alloc] init]; 
[URLArray addObject:@"http://google.nl"]; 
NSString *URLString = [NSString stringWithFormat:@"%@",[URLArray objectAtIndex:indexPath.row]]; 
NSLog(@"URLString: %@",URLString.description); 
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:URLString] options:@{} completionHandler:nil]; 

您可以在NSMutableArray添加URLs但要確保 URLArray.count == numberOfRowsInSection避免NSRangeException

+1

查看最新評論。它是固定的 – Anoniem

相關問題