gpt4 book ai didi

objective-c - iOS 单元格标签链接

转载 作者:行者123 更新时间:2023-11-28 22:55:11 25 4
gpt4 key购买 nike

大家好,我是 iOS 编程的新手,我需要一些帮助。

我有一个 TableView 单元格,它由 .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:我是这方面的新手,所以请描述一下。谢谢

最佳答案

首先,将以下内容移动到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]];
}
}

关于objective-c - iOS 单元格标签链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10947160/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com