0

在我的应用程序中,我有一个包含 120 行的 UITableView,每行都有 1 个 UItextfeilds 和 1 个按钮,如下面的代码所示:

-(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];
}
else
{

}

UITextField *NameTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 10, 230, 28)];
NameTextField.borderStyle = UITextBorderStyleRoundedRect;

NameTextField.delegate = self;
NameTextField.textColor = UIColorFromRGB(0x2A1807);
NameTextField.font = [UIFont fontWithName:@"Helvetica" size:(17.0)];
NameTextField.font = [UIFont boldSystemFontOfSize:20];

NameTextField.tag = [indexPath row ];
NSString *temp = [self.sectionNames objectAtIndex:[indexPath section]];
NameTextField.text = [[self.myDataArray objectForKey:temp] objectAtIndex:[indexPath row]];
[cell.contentView addSubview:NameTextField];
[NameTextField release];
UIButton * aBtn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *wijzigImage = [UIImage imageNamed:@"btn_delete.png"];
aBtn.frame = CGRectMake(240, 10, 28, 26);
[aBtn setImage:wijzigImage forState:UIControlStateNormal];
[aBtn addTarget:self action:@selector(deleteCustomCellWithUIButton:) forControlEvents:UIControlEventTouchUpInside];


[cell.contentView addSubview:aBtn];
return cell;

}

我注意到滚动很慢而且不流畅。

任何想法 ?

谢谢

4

2 回答 2

1

那是因为您每次都创建文本字段和按钮,将其添加到if (cell == nil) {...}. 唯一应该留在外面的if是 textField 文本设置。

顺便说一句,您的文本字段泄漏。

于 2012-04-19T18:41:21.950 回答
1

我找到了解决方案:

if (cell == nil) {
    cell = [[[UITableViewCell alloc] 
             initWithStyle:UITableViewCellStyleDefault
             reuseIdentifier:CellIdentifier] autorelease];
}
else
{
UITextField *oldTextField = (UITextField *)[cell.contentView viewWithTag:999];
        [oldTextField removeFromSuperview];
        UIButton *oldBtn = (UIButton *)[cell.contentView viewWithTag:888];
        [oldBtn removeFromSuperview];
}
   NameTextField.tag = 999;

aBtn.tag = 88;
于 2012-04-19T18:58:11.717 回答