0

嗨,我是 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:我是这个东西的新手,所以要描述一下。谢谢

4

2 回答 2

1

首先,将以下内容移至 viewDidLoad

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

其次,对于值为@"Link"的单元格

- (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]];
    }
}
于 2012-06-08T10:36:34.053 回答
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];

    }
}
于 2012-06-08T10:44:38.143 回答