您好我是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 :(得分: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]];
}
}
答案 1 :(得分: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];
}
}