gpt4 book ai didi

ios - UITableView 重复前两个值

转载 作者:行者123 更新时间:2023-11-29 02:37:53 25 4
gpt4 key购买 nike

我有两个并排的不同表格,允许用户选择月份和年份组合。每个表都填充了来自两个不同数组的值。 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com