我只是显示UITableView
UIWebView
,我的代码是
-(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
的{{1}}
为什么会这样?
答案 0 :(得分:0)
这种情况正在发生,因为一旦你初始化了细胞,它们就会以不同的顺序被摧毁。 iOS会自动执行此操作。 如果您希望它们停止取消过程,请每次都启动它们,或者单独处理它们。
这是我的意思的一个例子:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *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;
}
答案 1 :(得分:-1)
我认为问题来自于使用“dequeueReusableCellWithIdentifier”:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
您应该将所有访问过的单元格存储在NSMutableDictionary中,然后您可以使用它们:
NSNumber * cellKey = [NSNumber numberWithInt:indexPath.row];
UITableViewCell *cell = [allCells objectForKey:cellKey];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
// add all you need to cell
// save cell in dictionary
[allCells setObject:cell forKey:cellKey];
}