Reputation: 21237
I have two different tables side by side to allow a user to select a month and year combination. Each table is populated with values from two different arrays. monthsArray has values '01' - '12' and yearsArray has values '2014' - '2023'. I have verified that the arrays hold the correct values.
The table view height is only large enough to display a little over one row at a time. As the user slides the table up to reveal subsequent values, the values at array places 0 and 1 repeat in both tables. For example, as the months table is scrolled up, this sequence repeats: 01 02 01 02 01 02... The correct number of total rows (12) are presented, but the values are wrong after the 2nd position. Here is a screen shot:
Here is the code:
- (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;
}
Here is the NSLog output as I scroll through the left table:
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}
EDIT: The comments below are correct, but only partially solved my issue. Now as I scroll through the table, the text seems to get rendered on top of the previous text. By the time I get to the last cell, it's a jumbled mess:
Upvotes: 0
Views: 315
Reputation: 15512
I edit you're code just try.
- (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;
}
Upvotes: 1