gpt4 book ai didi

iphone - UITableView reloadData 方法造成数据模糊的麻烦

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:17:44 26 4
gpt4 key购买 nike

我有一个 UITableView,其中每个 UITableViewCell 上面都有一个 UIView 以及上面的 UIView 我有两个 UIButton 和两个 UILabel。我的问题是,当我重新加载 tableview 时,新数据被重写在旧数据上,使内容模糊不清,在方法 cellForRowAtIndexPath 中,我使用了删除标签的代码,然后它也不起作用妥妥的......

NSArray *subviews = [[NSArray alloc] initWithArray:cell.contentView.subviews];
for (UILabel *subview in subviews) {
[subview removeFromSuperview];
}
[subviews release];

此代码一次显示一行,否则没有人...

cellForRowAtIndexPath 的代码是这样的....

- (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)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 action:@selector(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 action:@selector(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 action:@selector(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: 因为我希望每个标签和按钮标签都应该相同,因为我必须在其他地方获取它们。

最佳答案

发生这种情况是因为您做错了。您甚至不需要重新加载表格。只需四处滚动,您就会注意到它。
每次显示一个单元格时,您都会创建新的标签和按钮。当然,这些 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com