ios - UITableView 重复前两个值

标签 ios objective-c uitableview

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

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

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

最佳答案

我编辑你的代码试试。

- (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;
}

关于ios - UITableView 重复前两个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26150332/

相关文章:

ios - 每次按下同一行时,didSelectRowAtIndexPath 都会更改 indexPath 值吗?

objective-c - 具有自定义字体的 UILabel 在 iOs 5 模拟器中不显示自定义字体

objective-c - NSUserDefaults、Settings.bundle 和 App 组

cocoa-touch - 如何从 UITableView 中取消选择一个对象?

ios - 如何使用 sharedHTTPCookieStorage 删除 cookie 数据和用户默认值?

objective-c - 如何在 iPhone SDK 的 Objective-C 中合并两个 UIImage 对象

ios - 向 UIScrollViewDelegate 方法添加代码

iOS:以编程方式将 UINavigationBar 添加到 UITableView

ios - 无法从 firebase 中删除图像?

iphone - 如何在 iOS 设备上将 mov 转换为 mp4