【问题标题】:Enhance UITableView Scrolling增强 UITableView 滚动
【发布时间】:2012-04-19 18:24:15
【问题描述】:

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

}

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

有什么想法吗?

谢谢

【问题讨论】:

    标签: iphone objective-c xcode uitableview scroll


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      我找到了解决方案:

      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;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-09
        • 1970-01-01
        • 2010-11-01
        • 2014-04-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多