UITableView重复前两个值

时间:2014-10-01 20:45:15

标签: ios objective-c uitableview

我有两个不同的表并排允许用户选择月份和年份组合。每个表都填充了两个不同数组的值。 monthsArray的值为'01' - '12',yearsArray的值为'2014' - '2023'。我已经验证了数组的值是否正确。

表格视图高度仅足以一次显示一行多一点。当用户向上滑动表以显示后续值时,数组中的值将在两个表中重复0和1。例如,当月份表向上滚动时,此序列重复:01 02 01 02 01 02 ...显示正确的总行数(12),但在第2个位置后值不正确。这是一个屏幕截图:

enter image description here

以下是代码:

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView.frame.size.width == MONTH_COLUMN_WIDTH) return 12;
    else return [self.yearsArray count];
}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *simpleTableIdentifier = @"SimpleTableItem";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    NSLog(@"indexPath: %@", indexPath);
    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
        cell.backgroundColor = [UIColor colorWithRed:DARK_BLUE_RED_COMPONENT green:DARK_BLUE_GREEN_COMPONENT blue:DARK_BLUE_BLUE_COMPONENT alpha:1.0f];
        cell.selectionStyle = UITableViewCellSelectionStyleDefault;

        // add a label for the segment name
        UILabel *label;

        if (tableView.frame.size.width == MONTH_COLUMN_WIDTH) {
            // this is the table to select the expiration month
            label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,MONTH_COLUMN_WIDTH,EXPIRATION_ROW_HEIGHT)];
            label.text = [self.monthsArray objectAtIndex:indexPath.row];
            label.textAlignment = NSTextAlignmentCenter;
        } else {
            // this is the table to select the expiration year
            label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,YEAR_COLUMN_WIDTH,EXPIRATION_ROW_HEIGHT)];
            label.text = [self.yearsArray objectAtIndex:indexPath.row];
            label.textAlignment = NSTextAlignmentCenter;
        }

        [label setFont:[UIFont fontWithName:DEFAULT_FONT size:13.0f]];
        label.textColor = [UIColor whiteColor];

        [cell addSubview:label];
    }

    return cell;
}

当我滚动左表时,这是NSLog输出:

  

indexPath:{length = 2,path = 0 - 0}
  indexPath:{length = 2,path = 0 - 1}
  indexPath:{length = 2,path = 0 - 2}
  indexPath:{length = 2,path = 0 - 3}
  indexPath:{length = 2,path = 0 - 4}
  indexPath:{length = 2,path = 0 - 5}
  indexPath:{length = 2,path = 0 - 6}
  indexPath:{length = 2,path = 0 - 7}
  indexPath:{length = 2,path = 0 - 8}
  indexPath:{length = 2,path = 0 - 9}
  indexPath:{length = 2,path = 0 - 10}
  indexPath:{length = 2,path = 0 - 11}

编辑:以下评论是正确的,但只是部分解决了我的问题。现在,当我滚动表格时,文本似乎呈现在前一个文本之上。当我到达最后一个单元格时,这是一个乱七八糟的混乱:

enter image description here

1 个答案:

答案 0 :(得分:1)

我编辑你的代码只是试试。

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *simpleTableIdentifier = @"SimpleTableItem";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];


  NSLog(@"indexPath: %@", indexPath);
    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
     cell.selectionStyle = UITableViewCellSelectionStyleDefault;
    }

        cell.backgroundColor = [UIColor colorWithRed:DARK_BLUE_RED_COMPONENT green:DARK_BLUE_GREEN_COMPONENT blue:DARK_BLUE_BLUE_COMPONENT alpha:1.0f];


        // add a label for the segment name
        UILabel *label;

        if (tableView.frame.size.width == MONTH_COLUMN_WIDTH) {
            // this is the table to select the expiration month
            label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,MONTH_COLUMN_WIDTH,EXPIRATION_ROW_HEIGHT)];
            label.text = [self.monthsArray objectAtIndex:indexPath.row];
            label.textAlignment = NSTextAlignmentCenter;
        } else {
            // this is the table to select the expiration year
            label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,YEAR_COLUMN_WIDTH,EXPIRATION_ROW_HEIGHT)];
            label.text = [self.yearsArray objectAtIndex:indexPath.row];
            label.textAlignment = NSTextAlignmentCenter;
        }

        [label setFont:[UIFont fontWithName:DEFAULT_FONT size:13.0f]];
        label.textColor = [UIColor whiteColor];

        [cell addSubview:label];

    return cell;
}