增强UITableView滚动

时间:2012-04-19 18:24:15

标签: iphone objective-c xcode uitableview scroll

在我的应用程序中,我有一个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;

}

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

任何想法?

谢谢

2 个答案:

答案 0 :(得分:1)

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

顺便说一句,你的文本域泄露了。

答案 1 :(得分: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;