[英]I’m not sure how to make a UITableView Cell open a link in safari?
I am very new to iOS development and Objective-C in general, and I'm not sure what to do here. 一般来说,我对iOS开发和Objective-C还是陌生的,我不确定该怎么做。 Basically, I have a UITableView set up so that each cell gets a title and description (two labels placed inside the prototype cell).
基本上,我设置了UITableView,以便每个单元格都有一个标题和描述(在原型单元格内放置了两个标签)。 I can use an array to control which cell displays what text.
我可以使用数组来控制哪个单元格显示什么文本。 However, now I want to use this same array system to make it so that each cell opens a different safari page.
但是,现在我想使用相同的阵列系统来制作它,以便每个单元打开一个不同的野生动物园页面。
Here's my TableViewController.h file: 这是我的TableViewController.h文件:
@interface MoreTableViewController : UITableViewController
@property (nonatomic, strong) NSArray *Title;
@property (nonatomic, strong) NSArray *Description;
Here's my TableViewController.m file: 这是我的TableViewController.m文件:
- (void)viewDidLoad
{
[super viewDidLoad];
_Title = @[@“Title1",
@“Title2",
@“Title3",];
_Description = @[@“Description1",
@“Description2",
@“Description3",];
}
Here's my Cell.h file: 这是我的Cell.h文件:
@interface MoreCell : UITableViewCell
@property (strong, nonatomic) IBOutlet UILabel *TitleLabel;
@property (strong, nonatomic) IBOutlet UILabel *DescriptionLabel;
Here's my Cell.m file: 这是我的Cell.m文件:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)awakeFromNib
{
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
Using the storyboard editor in Xcode 5 I was able to link the DescriptionLabel and TitleLabel to some labels so that multiple cells could be created and the labels could be assigned based on the array. 使用Xcode 5中的情节提要编辑器,我可以将DescriptionLabel和TitleLabel链接到某些标签,以便可以创建多个单元格并可以根据数组分配标签。
Heres a screenshot: https://www.dropbox.com/s/25rrez8y2071m63/Screen%20Shot%202014-07-19%20at%2012.49.02%20PM.png 继承人截图: https : //www.dropbox.com/s/25rrez8y2071m63/Screen%20Shot%202014-07-19%20at%2012.49.02%20PM.png
My main goal is just to make each cell open a different webpage using an array like the one above. 我的主要目标是使用上述数组,使每个单元打开一个不同的网页。
Any help is greatly appreciated. 任何帮助是极大的赞赏。
-Austin -奥斯丁
In completion of Malex reply: 在完成Malex回复时:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Get your url, eg: NSString *url = yourArray[indexPath.row];
NSString *url = @"http://www.apple.com";
[[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]];
}
You need to implement 您需要实施
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
delegate method and choose appropriate url from your _URL array which also must be defined in your 委托方法,然后从_URL数组中选择适当的url,这也必须在您的
- (void)viewDidLoad
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.