我正在为iphone应用程序做一个注册表。在那里我有一个带有四个部分的tableview。在最后一部分我有一行带有注册按钮。我得到了数据很好的tableview。但在上下滚动tableview我的注册按钮转到第二部分。我添加了下面的代码我在这做了什么错。请帮我解决。
- (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;
}
答案 0 :(得分:1)
它会重复,因为您将其添加到单元格的内容视图中,然后重新使用该单元格。 不是将其添加到单元格的contentView中,而是将按钮添加为单元格附件。
cell.accessoryView = btn_register;
在所有其他单元格中,将accessoryView设置为nil。
cell.accessoryView = nil;
答案 1 :(得分:0)
为什么不尝试为每个单元格区域设置不同的标识符。您还可以使用“使用[tableView dequeueReusableCellWithIdentifier:forIndexPath:
答案 2 :(得分:0)
使用“注册”单元格的不同单元格标识符应该有效。如果您使用容器来保存数据,那么您的代码将会非常清晰,例如NSDictionary
。例如:
NSArray *account = @[@"Email", @"Password", @"Confirm password"];
NSArray *userInfo = @[@"First name", @"Last name", @"Gender", @"Nickname"];
// ...
NSDictionary *data = @{@"Account": account, @"User Information": userInfo /* ... */};