gpt4 book ai didi

iphone - 表格 View 上下滚动时单元格顺序发生变化

转载 作者:行者123 更新时间:2023-11-29 13:10:46 26 4
gpt4 key购买 nike

我正在为 iphone 应用程序做一个注册表。在那里有一个包含四个部分的表格 View 。在最后一节中有一行带有注册按钮。我很好地得到了带有数据的表格 View 。但是在上下滚动表格 View 时,我的注册按钮转到了第二部分。我添加了下面的代码.我做错了什么。帮我解决一下。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath 
{
// static NSString *CellIdentifier = @"Cell";
// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSString *identifier = @"reuseIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleGray;

NSLog(@" row and section ===== %d,%d",indexPath.row,indexPath.section);
if(indexPath.section != 3)
{
UILabel *lbl_title = [[UILabel alloc] initWithFrame:(isiPad?CGRectMake(7,10,250,50):CGRectMake(70,40,340, 35))];
[lbl_title setFont:[UIFont fontWithName:@"Verdana-Bold" size:(isiPad?20:13)]];
lbl_title.backgroundColor = [UIColor clearColor];
[lbl_title setTextColor:[UIColor blackColor]];

lbl_title.tag = ((indexPath.section+1)*10)+indexPath.row;
//NSLog(@"lbl data tag %d",lbl_title.tag);

lbl_title.textAlignment = UITextAlignmentLeft;
[cell.contentView addSubview:lbl_title];

UILabel *lbl_data = [[UILabel alloc] initWithFrame:(isiPad?CGRectMake(260,10,410,50):CGRectMake(70,40,340, 35))];
[lbl_data setFont:[UIFont fontWithName:@"Verdana-Bold" size:(isiPad?20:13)]];
lbl_data.backgroundColor = [UIColor clearColor];//25-25-112
[lbl_data setTextColor:[UIColor colorWithRed:25.0/255.0 green:25.0/255.0 blue:112.0/255.0 alpha:1.0]];
lbl_data.textAlignment = UITextAlignmentCenter;
lbl_data.tag = ((indexPath.section+1)*100)+indexPath.row;
//NSLog(@"lbl data tag %d,%@",lbl_data.tag,lbl_data);
[cell.contentView addSubview:lbl_data];

if(indexPath.section == 0)
{
if(indexPath.row == 0)
{
lbl_title.text = @"Email";
}
else if(indexPath.row == 1)
{
lbl_title.text = @"Password";

}
else if(indexPath.row == 2)
{
lbl_title.text = @"Confirm password";
}
}
else if(indexPath.section == 1)
{
if(indexPath.row == 0)
{
lbl_title.text = @"First name";
}
else if(indexPath.row == 1)
{
lbl_title.text = @"Last name";

}
else if(indexPath.row == 2)
{
lbl_title.text = @"Nickname";
}
else if(indexPath.row == 3)
{
lbl_title.text = @"Gender";
}
}
else if(indexPath.section == 2)
{
if(indexPath.row == 0)
{
lbl_title.text = @"City";
}
else if(indexPath.row == 1)
{
lbl_title.text = @"State";

}
else if(indexPath.row == 2)
{
lbl_title.text = @"Country";
}
}

}
if(indexPath.section == 3)
{
if(indexPath.row == 0)
{
cell.backgroundColor = [UIColor colorWithRed:165.0/255.0 green:42.0/255.0 blue:42.0/255.0 alpha:1.0];//165-42-42
UIButton *btn_register = [[UIButton alloc] initWithFrame:isiPad?CGRectMake(10,0,660,70):CGRectMake(0,0,320,40)];
[btn_register addTarget:self action:@selector(btn_register_clicked:) forControlEvents:UIControlEventTouchUpInside];
[btn_register setTitle:@"Register" forState:UIControlStateNormal];
//btn_register.titleLabel.textAlignment = UITextAlignmentCenter;
btn_register.backgroundColor = [UIColor blueColor];
[btn_register setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[btn_register setFont:[UIFont fontWithName:@"Verdana-Bold" size:24]];
[cell.contentView addSubview:btn_register];
}
}

}
else
{
NSLog(@"updating cell.........");
}
return cell;

}

before scrolling up and down

after scrolling

最佳答案

它会重复,因为您将它添加到单元格的内容 View 中,然后您重新使用该单元格。不要将它添加到单元格的 contentView 中,而是将按钮添加为单元格附件。

cell.accessoryView = btn_register;

在所有其他单元格中,将 accessoryView 设置为 nil。

cell.accessoryView = nil;

关于iphone - 表格 View 上下滚动时单元格顺序发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17440334/

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