我想在自定义表格单元格中使用UIWebView
和label
显示一些网页。
我已成功为不同的cell
获取不同的名称,但我在每个自定义url
中获得相同的cell
。如何为不同的单元格获得不同的URL
?
//MyCustomCell.h
@property (weak, nonatomic) IBOutlet UIWebView *myWebView;
@property (weak, nonatomic) IBOutlet UILabel *websiteNameLabel;
@property NSString *websiteURLString;
//MyCustomCell.m
- (void)awakeFromNib
{
// Initialization code
self.websiteURLString = [NSURL URLWithString:@"http://www.google.com"];
NSURLRequest *request1 = [NSURL URLWithString:self.websiteURLString];
[[self myWebView]loadRequest:request1];
self.websiteURLString = [NSURL URLWithString:@"http://www.gmail.com"];
NSURLRequest *request2 = [NSURL URLWithString:self.websiteURLString];
[[self myWebView]loadRequest:request2];
self.websiteURLString = [NSURL URLWithString:@"http://www.facebook.com"];
NSURLRequest *request3 = [NSURL URLWithString:self.websiteURLString];
[[self myWebView]loadRequest:request3];
}
//MyViewController.h
@property NSMutableArray *webPageNameArray;
@property NSString *googleWebURL,*yahooWebURL,*facebookWebURL;
//MyViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.webPageNameArray = [[NSMutableArray alloc]initWithObjects:@"google",@"gmail",@"facebook", nil];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *identifier = @"myCell";
StudentCustomCell *cell = (StudentCustomCell*)[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"StudentCustomCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.websiteNameLabel.text = [self.webPageNameArray objectAtIndex:indexPath.row];
return cell;
}
答案 0 :(得分:0)
从-awakeFromNib
MyCustomCell.m
删除您的代码,然后使用以下内容替换MyViewController.m
处的代码:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.webPageNameArray = [ @"google", @"gmail", @"facebook" ];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.webPageNameArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = @"myCell";
StudentCustomCell *cell = (StudentCustomCell*)[tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell)
{
UINib *headerNib = [UINib nibWithNibName:@"StudentCustomCell" bundle:nil];
NSArray *aNib = [headerNib instantiateWithOwner:self options:nil];
cell = [aNib objectAtIndex:0];
}
cell.websiteNameLabel.text = self.webPageNameArray[indexPath.row];
NSString *websiteURLString = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.%@.com", self.webPageNameArray[indexPath.row]]];
NSURLRequest *request = [NSURL URLWithString:websiteURLString];
[cell.myWebView loadRequest:request];
return cell;
}
答案 1 :(得分:0)
NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"%@",cell.websiteURLString]];
NSURLRequest *myRequest=[NSURLRequest requestWithURL:url];
[cell.myWebView loadRequest:myRequest];