Objective-c:[self.tableview reloadData] 重新加载数据但不清除旧单元格

发布于 2024-10-16 07:45:11 字数 2351 浏览 8 评论 0原文

我正在开发一个tableview,我是动态创建单元格的新手(我曾经用IB创建它们,然后将它们链接到他们的tableviewcell控制器等)。

一切都很好,并且正确更新了预期的数组,但是当我触发 [self.tableview reloadData] 时,程序只是在旧单元格上重新绘制新值。例如,如果单元格内的 uilabel 中有“TEST CELL”值,当我将数据更新为“CELL TEST”并触发 reloadData 时,uilabel 看起来就像有两个标签彼此重叠,并且两个值都是可见的。 (这就像创建两个位置完全相同、大小相同的 uilabel 并设置它们的值)

并且每次我触发 reloadData 时都会发生此事件,每次重新加载时,程序看起来就像在旧的 uilabel 之上添加了另一个 uilabel。这是我的代码:

- (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] autorelease];
}
UILabel *lblText1 = [[UILabel alloc] initWithFrame:CGRectMake(30, 5, 130, 30)];
lblText1.adjustsFontSizeToFitWidth = YES;
lblText1.backgroundColor = [UIColor clearColor];
lblText1.text = [lblText1Array objectAtIndex:indexPath.row];
[cell addSubview:lblText1];
[lblText1 release];
if(indexPath.row<3)
{   
    UILabel *lblText2 = [[UILabel alloc] initWithFrame:CGRectMake(170, 5, 130, 30)];
    lblText2.adjustsFontSizeToFitWidth = YES;
    lblText2.backgroundColor = [UIColor clearColor];
    lblText2.text = nil;
    lblText2.text = [spParameters objectAtIndex:indexPath.row];
    [cell addSubview:lblText2];
    [lblText2 release];
}
else if(indexPath.row==3){
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(170, 7, 130, 30)];
    textField.adjustsFontSizeToFitWidth = YES;
    textField.borderStyle = UITextBorderStyleRoundedRect;
    textField.placeholder = @"please insert value";
    textField.textAlignment = UITextAlignmentCenter;
    textField.delegate = self;
    textField.text = [spParameters objectAtIndex:indexPath.row];
    [cell addSubview:textField];
    [textField release];
}
else if(indexPath.row == 4)
{
    UISwitch *gSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(170, 7, 130, 30)];
    [gSwitch setOn:FALSE];
    [gSwitch addTarget: self action: @selector(switchValueChanged:) forControlEvents:UIControlEventValueChanged];
    [cell addSubview:gSwitch];
    [gSwitch release];
}
// Configure the cell...
return cell;

}

我在将组件添加到子视图后释放它们,并且我正在考虑重用标识符是否有问题......

感谢您的帮助。

i'm working on a tableview and i am newbie in dynamically creating cells (i used to create them with IB and then link them to their tableviewcell controller etc..).

Everything works great and recpected arrays are updated properly but when i fire [self.tableview reloadData] the program just redraws new values over old cells. for example if there "TEST CELL" value in a uilabel inside a cell, when i update the data to "CELL TEST" and fire the reloadData, the uilabel looks like there are two labels on top of each other and both values are visible. (its like creating two uilabel with exact same location and same size and setting their values)

and this event happens everytime i fire reloadData, with each reload, the program looks like its adding another uilabel on top of the older one. heres my code:

- (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] autorelease];
}
UILabel *lblText1 = [[UILabel alloc] initWithFrame:CGRectMake(30, 5, 130, 30)];
lblText1.adjustsFontSizeToFitWidth = YES;
lblText1.backgroundColor = [UIColor clearColor];
lblText1.text = [lblText1Array objectAtIndex:indexPath.row];
[cell addSubview:lblText1];
[lblText1 release];
if(indexPath.row<3)
{   
    UILabel *lblText2 = [[UILabel alloc] initWithFrame:CGRectMake(170, 5, 130, 30)];
    lblText2.adjustsFontSizeToFitWidth = YES;
    lblText2.backgroundColor = [UIColor clearColor];
    lblText2.text = nil;
    lblText2.text = [spParameters objectAtIndex:indexPath.row];
    [cell addSubview:lblText2];
    [lblText2 release];
}
else if(indexPath.row==3){
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(170, 7, 130, 30)];
    textField.adjustsFontSizeToFitWidth = YES;
    textField.borderStyle = UITextBorderStyleRoundedRect;
    textField.placeholder = @"please insert value";
    textField.textAlignment = UITextAlignmentCenter;
    textField.delegate = self;
    textField.text = [spParameters objectAtIndex:indexPath.row];
    [cell addSubview:textField];
    [textField release];
}
else if(indexPath.row == 4)
{
    UISwitch *gSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(170, 7, 130, 30)];
    [gSwitch setOn:FALSE];
    [gSwitch addTarget: self action: @selector(switchValueChanged:) forControlEvents:UIControlEventValueChanged];
    [cell addSubview:gSwitch];
    [gSwitch release];
}
// Configure the cell...
return cell;

}

im releasing the components after i adding them to subviews, and i am thinking about if theres something wrong with the reuseidentifier...

Thanx for helping.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

混吃等死 2024-10-23 07:45:11

看起来好像您正在使用固定数量的单元格填充详细信息视图,因此您应该考虑静态创建实例,例如在 viewDidLoad 或 Interface Builder 中。您可以将每个单元格存储在单独的实例变量中,并在每次调用 tableView:cellForRowAtIndexPath: 时返回与当前行对应的单元格。

如果您以编程方式创建单元格,请添加当时需要的任何子视图。否则,正如我提到的,您可以在 Interface Builder 中执行此操作,这通常可以更轻松地设置控件的详细信息(例如文本字段)。请注意,尽管 UITableViewCell 已经包含 UILabel 的实例,因此自行添加一个实例是多余的。相反,只需访问单元格的 textLabel 属性即可获取其标签。

It looks as though you're populating a detail view with a fixed number of cells, so you should consider creating the instances statically, for example in viewDidLoad or in Interface Builder. You could store each cell in a separate instance variable, and just return the one that corresponds the current row each time tableView:cellForRowAtIndexPath: is called.

If you create the cells programmatically, add whatever subviews you need at that time. Otherwise, as I mentioned, you could do that in Interface Builder, which often makes it easier to set up the details of controls such as text fields. Note though that UITableViewCell already contains an instance of UILabel, so adding one yourself is redundant. Instead, just access the cell's textLabel property to get its label.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文