我只是显示一个UITableView
with UIWebView
s 而我的代码是
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 5, 400, 30)];
[lable setBackgroundColor:[UIColor clearColor]];
[lable setTextColor:[UIColor darkGrayColor]];
lable.tag = 333;
[lable setFont:[UIFont fontWithName:@"Helvetica-Bold" size:16.0f]];
//[lable setText:[arrayOfSectionTitles objectAtIndex:indexPath.row]];
[cell addSubview:lable];
if (indexPath.row == 0) {
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(15, 45, 600, 250)];
webView.tag = 1001;
[webView setBackgroundColor:[UIColor greenColor]];
NSString *urlAddress = @"http://www.roseindia.net";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
[cell.contentView addSubview:webView];
[webView setHidden:YES];
}
else if (indexPath.row == 1){
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(15, 45, 600, 100)];
webView.tag = 1002;
[webView setBackgroundColor:[UIColor greenColor]];
NSString *urlAddress = @"http://www.roseindia.net";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
[cell.contentView addSubview:webView];
[webView setHidden:YES];
}
else if (indexPath.row == 2){
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(15, 45, 600, 200)];
webView.tag = 1003;
[webView setBackgroundColor:[UIColor greenColor]];
NSString *urlAddress = @"http://www.roseindia.net";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
[cell.contentView addSubview:webView];
[webView setHidden:YES];
}
}
UILabel* label = (UILabel*)[cell viewWithTag:333];
[label setText:[arrayOfSectionTitles objectAtIndex:indexPath.row]];
tableview.separatorColor = [UIColor clearColor];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
BOOL isSelected = ![self cellIsSelected:indexPath];
// Store cell 'selected' state keyed on indexPath
NSNumber *selectedIndex = [NSNumber numberWithBool:isSelected];
[selectedIndexes setObject:selectedIndex forKey:indexPath];
UITableViewCell *cell = (UITableViewCell *)[tableview cellForRowAtIndexPath:indexPath];
if (indexPath.row == 0) {
UIWebView* webView = (UIWebView*)[cell viewWithTag:1001];
if (!cellSelected1) {
[webView setHidden:NO];
cellSelected1 = YES;
}
else {
[webView setHidden:YES];
cellSelected1 = NO;
}
}
else if (indexPath.row == 1) {
UIWebView* webView = (UIWebView*)[cell viewWithTag:1002];
if (!cellSelected2) {
[webView setHidden:NO];
cellSelected2 = YES;
}
else {
[webView setHidden:YES];
cellSelected2 = NO;
}
}
else if (indexPath.row == 2) {
UIWebView* webView = (UIWebView*)[cell viewWithTag:1003];
if (!cellSelected3) {
[webView setHidden:NO];
cellSelected3 = YES;
}
else {
[webView setHidden:YES];
cellSelected3 = NO;
}
}
[tableview reloadData];
}
当我单击第一个单元格时,UIWebView
应该显示,再次单击以隐藏...它正在工作。
但我的问题是,如果我滚动UITableView
,内容(UIWebView
)正在改变它indexPath
并显示在最后indexPath
一个tableview
.
为什么会这样?