我有一个 UITableView ,其中每个 UITableViewCell 上都有一个 UIView 及以上 UIView 我有两个 UIButton 和两个 UILabel 。我的问题是,当我重新加载表格 View 时,新数据被重写在使内容模糊不清的旧数据上,并且在方法 cellForRowAtIndexPath 我使用了这个删除标签的代码然后它也不起作用正确.......
NSArray *subviews = [[NSArray alloc] initWithArray:cell.contentView.subviews];
for (UILabel *subview in subviews) {
[subview removeFromSuperview];
}
[subviews release];
此代码一次显示一行,否则没有人...
cellForRowAtIndexPath 的代码是这样的......
- (UITableViewCell *)tableViewUITableView *)tableView1 cellForRowAtIndexPathNSIndexPath *)indexPath {
static NSString *identifier = @"Cell";
UITableViewCell *cell = [tableView1 dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
NSDictionary *dict = [appDelegate.webDataList objectAtIndex:indexPath.row];
tableView.backgroundColor = [UIColor colorWithRed:0.39 green:0.39 blue:0.39 alpha:0.39];
if([appDelegate.dataArray count]>0){
imgView.backgroundColor = [UIColor clearColor];
imgView =[[UIView alloc ] initWithFrame:CGRectMake(0,0,320,45)];
imgView.tag= indexPath.row+250;
[cell.contentView addSubview:imgView];
button=[UIButton buttonWithType:UIButtonTypeCustom];
button.frame=CGRectMake(291, 5, 19, 21);
img =[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource"swap_re" ofType"png"]];
[button setBackgroundImage:img forState:UIControlStateNormal];
[button addTarget:self actionselector(swapButtonPressed forControlEvents:UIControlEventTouchUpInside];
button.tag= indexPath.row+250;
[imgView addSubview:button];
button=[UIButton buttonWithType:UIButtonTypeCustom];
button.frame=CGRectMake(-25, 5, 19, 21);
img =[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource"swap_re" ofType"png"]];
[button setBackgroundImage:img forState:UIControlStateNormal];
[button addTarget:self actionselector(swapButtonPressed forControlEvents:UIControlEventTouchUpInside];
button.tag= indexPath.row+250;
[imgView addSubview:button];
button=[UIButton buttonWithType:UIButtonTypeCustom];
button.frame=CGRectMake(5, 7, 19, 21);
button.tag = indexPath.row+250;
img =[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource"delete" ofType"png"]];
[button setBackgroundImage:img forState:UIControlStateNormal];
[button addTarget:self actionselector(deleteButtonPressed forControlEvents:UIControlEventTouchUpInside];
[imgView addSubview:button];
NSArray *arr = [[appDelegate.dataArray objectAtIndex:indexPath.row] componentsSeparatedByString"/"];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(34,9,140,20)];
label.text = [arr objectAtIndex:1];
label.tag = indexPath.row+250;
[label setFont:[UIFont fontWithName:@"Georgia" size:14]];
[label setNumberOfLines:0];
label.textAlignment = UITextAlignmentLeft;
label.textColor = [UIColor whiteColor];
label.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
label.backgroundColor = [UIColor clearColor];
[imgView addSubview:label];
[label release];
NSDictionary *dict1 =[appDelegate.webDataList objectAtIndex:indexPath.row];
label = [[UILabel alloc] initWithFrame:CGRectMake(181, 7, 75, 20)];
label.tag = indexPath.row+250;
label.text = [dict1 objectForKey:@"time"];
[label setFont:[UIFont fontWithName:@"Georgia" size:16.0]];
label.textAlignment = UITextAlignmentLeft;
[label setBackgroundColor:[UIColor clearColor]];
label.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
[label setTextColor:[UIColor whiteColor]];
[imgView addSubview:label];
[label release];
}
return cell;
}
在我的代码中,我无法使用 reuseTableViewCellWithIdentifier: 方法,因为我希望每个标签和按钮标签都应该相同,因为我必须在其他地方获取它们。
Best Answer-推荐答案 strong>
这是因为你做错了。您甚至不需要重新加载表格。只需滚动一下,您就会注意到它。
每次显示单元格时,您都会创建新的标签和按钮。当然,这些 UI 元素永远不会被删除。
在创建新单元格时添加 UI 元素并给它们一个标签。
如果单元格成功出列,则通过该标记获取元素并设置其值。
像这样:
if (cell == nil) {
// if no cell could be dequeued create a new one
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
// add label to the new cell
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(34,9,140,20)];
label.tag = 250; // <---- save tag
[label setFont:[UIFont fontWithName:@"Georgia" size:14]];
[label setNumberOfLines:0];
label.textAlignment = UITextAlignmentLeft;
label.textColor = [UIColor whiteColor];
label.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
label.backgroundColor = [UIColor clearColor];
//[imgView addSubview:label]; // I ignore this because I don't want to write 200 lines of code here. This is just an example so please adopt the code yourself
[cell.contentView addSubView:label];
[label release];
}
// whatever happened you have a cell with all the labels added here
UILabel *label = (UILabel *)[cell.contentView viewWithTag:250] // <---- get label with tag
label.text = [arr objectAtIndex:1];
而且您不需要 if([appDelegate.dataArray count]>0) 。如果所有其他 tableview 数据源方法都正确 tableView:cellForRowAtIndexPath: 如果您的数据源为空,则不会调用。
编辑:刚刚看到那个:
and in my code i am not able to use the method reuseTableViewCellWithIdentifier: because i want each label and button tag should be same because i have to fetch them else where.
其实你可以。每个人都可以而且应该使用 reuseTableViewCellWithIdentifier: .
您对此的具体问题是什么?
如果您需要在按钮操作方法中获取 indexPath(或者在您的情况下只有行),请使用我的 answer to another question 中的代码
关于iphone - UITableView reloadData 方法造成数据模糊的麻烦,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/8519026/
|