2012-06-08 39 views
0

嗨,我是iOS編程新手,需要一點幫助。iOS單元格標籤鏈接

我有一個表格視圖單元格由.plist文件中的數據填充。我需要能夠在其中一個單元格內建立鏈接。我怎麼能這樣做?

這裏是一個加載數據到細胞中的代碼:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"CustomTableCell"; 
    static NSString *CellNib = @"DetailViewCell"; 



    DetailViewCell *cell = (DetailViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellNib owner:self options:nil]; 
     cell = (DetailViewCell *)[nib objectAtIndex:0]; 
    } 

    cell.accessoryType = UITableViewCellAccessoryNone; 
    cell.cellTitleLabel.textColor = [UIColor blackColor]; 
    cell.cellTitleLabel.font = [UIFont systemFontOfSize:20.0]; 
    cell.cellSubtitleLabel.textColor = [UIColor darkGrayColor]; 

    informations = [[NSArray alloc] initWithObjects:@"City", @"Country", @"State", @"History", @"Link", nil]; 
    subtitles = [[NSArray alloc] initWithObjects:titleString, subtitleString, stateString, populationString, @"Link", nil]; 


    cell.cellTitleLabel.text = [informations objectAtIndex:indexPath.row]; 
    cell.cellSubtitleLabel.text = [subtitles objectAtIndex:indexPath.row]; 

    return (DetailViewCell *) cell; 
} 

對於小區「一線通」我需要它來打開存儲在該的.plist文件的URL。我怎麼能這樣做?

非常感謝

瑞安

PS:我在這個東西是一個新手,所以是描述性的。由於

+0

這是我可以建議的想法之一。創建一個新視圖由上面類的UIWebView&in'didSelectRowAtIndexPath'方法組成,您可以檢查該'鏈接'單元的'indexpath'。並在新創建的UIWebView中打開該單元格中的網址。 –

回答

1

所有的杉杉,移動以下到viewDidLoad中

informations = [[NSArray alloc] initWithObjects:@"City", @"Country", @"State", @"History", @"Link", nil]; 
    subtitles = [[NSArray alloc] initWithObjects:titleString, subtitleString, stateString, populationString, @"Link", nil]; 

其次,對於具有的值的單元格@「鏈接」

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    DetailViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    if(cell.cellTitleLabel.text == @"Link") 
    { 
      //Open in safari 
      [[UIApplication sharedApplication] openURL:[NSURL URLWithString:cell.cellSubtitleLabel.text]]; 
    } 
} 
+1

太好了,稍作調整,我設法讓這個工作。謝謝你的幫助! – ryryan

+0

歡迎您:) –

+0

關於iPad的UIActionsheets,我現在被別的東西卡住了。看到我的個人資料的問題。不用擔心,如果你不能幫助。 :) – ryryan

1

讓您的viewController的兩個陣列性能。

@property (nonatomic, retain) NSArray *informationArray, *subtitlesArray; 

初始化他們在你-viewDidLoad方法:

self.informationArray = [NSArray arrayWithObjects:@"City", @"Country", @"State", @"History", @"Link", nil]; 
    self.subtitlesArray = [NSArray arrayWithObjects:titleString, subtitleString, stateString, populationString, @"Link", nil]; 

,然後檢查是否檢索到的數據是一個鏈接,然後在Safari中打開它。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    id title = [self.informationArray objectAtIndex:indexPath.row]; 

    NSURL *link = [NSURL URLWithString:[self.subtitlesArray objectAtIndex:indexPath.row]]; 

    //or to get the link out of a .plist 

    NSDictionary *plist = [NSData dataWithContentsOfFile:filePathForPlist]; 

    NSURL *link = [NSURL URLWithString:[[plist objectForKey:title] objectForKey:@"Link"]; 

    // you may need to get a different object from the .plist depending on the structure of the file. 

    //Check to see if the indexPath matches a cell with a title of "Link" and that the URL can be opened. 

    if([title isEqualToString:@"Link"] && [[UIApplication sharedApplication] canOpenURL:link]) { 

      [[UIApplication sharedApplication] openURL:link]; 

    } 
}