【问题标题】:UITableview sections overwrite in iPhone?UITableview 部分在 iPhone 中覆盖?
【发布时间】:2011-12-01 11:59:38
【问题描述】:

我在 UITableview 分组样式中的两个部分有一个问题。第一部分有 6 行,第二部分有 3 行。当我上下滚动表格视图时,第 2 部分的最后一行内容添加到第一部分的第一行,第一部分的第一行内容添加到第二部分的最后一行。我最好检查我的级别,单元格是否为空以加载内容,但是在调试时没有发生。我的代码是,

- (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];
            cell.backgroundColor = [UIColor whiteColor];

            if (indexPath.section == 0 && indexPath.row == 0) 
            {
                UILabel *Nameabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 140, 30)];
                Nameabel.text = @"Name";
                Nameabel.font = [UIFont fontWithName:@"Helvetica" size:15];
                Nameabel.backgroundColor = [UIColor clearColor];
                Nameabel.font = [UIFont boldSystemFontOfSize:15];
                [cell.contentView addSubview:Nameabel];

                textField = [[UITextField alloc] initWithFrame:CGRectMake(145, 5, 150, 30)];
                textField.placeholder = @"Student Name";
                textField.borderStyle = UITextBorderStyleNone;
                textField.font = [UIFont fontWithName:@"Helvetica" size:14];
                [cell.contentView addSubview:paymentCatTextField];

            }
            else if (indexPath.section == 1 && indexPath.row == 0) 
            {
                UILabel *RLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 140, 30)];
                RLabel.text = @"Class";
                RLabel.backgroundColor = [UIColor clearColor];
                RLabel.font = [UIFont fontWithName:@"Helvetica" size:15];
                RLabel.font = [UIFont boldSystemFontOfSize:15];
                [cell.contentView RLabel];

                RTextField = [[UITextField alloc] initWithFrame:CGRectMake(145, 5, 150, 30)];
                RTextField.placeholder = @"class Name";
                RTextField.borderStyle = UITextBorderStyleNone;
                RTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
                RTextField.font = [UIFont fontWithName:@"Helvetica" size:14];
                [cell.contentView addSubview:RTextField];
            }
        }
       return cell;
    }

这是示例。通过这种方式,第 0 部分有 6 行,第 1 部分有 3 行。我在哪里做错了。请帮我解决这个问题。

提前致谢。

【问题讨论】:

  • cells 可重复使用以提高性能,你应该仔细阅读苹果的文档,UITableView 的部分基本和重要。

标签: iphone uitableview sections


【解决方案1】:

将您的代码更改为以下内容,然后重试:

{
 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.backgroundColor = [UIColor whiteColor];
    }


    if (indexPath.section == 0 && indexPath.row == 0) {
         //bla bla
    } else if (indexPath.section == 1 && indexPath.row == 0) {
         //bla bla
    }

    return cell;
}

原因是您仅在不重复使用单元格时才设置单元格内容。在大多数情况下这是错误的。

【讨论】:

    【解决方案2】:

    除了 dasdom 的回答,我还建议在添加标签之前删除子视图。

    if ([[cell.contentView subviews] count] > 0) {
        UIView *labelToClear = [[cell.contentView subviews] objectAtIndex:0];
        [labelToClear removeFromSuperview];
        UIView *textToClear = [[cell.contentView subviews] objectAtIndex:1];
        [textToClear removeFromSuperview];
    }
    

    并自动释放子视图:

    UILabel *Nameabel = [[[UILabel alloc] initWithFrame:CGRectMake(5, 5, 140, 30)] autorelease];
    UITextField *textField = [[[UITextField alloc] initWithFrame:CGRectMake(145, 5, 150, 30)] autorelease];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多