Ios Iphone中的分组表视图
我在应用程序中使用分组表视图。表中有两个部分。第一节有3行,第二节有10多行。当我滚动节时,它会在第二节第5行之后显示第1节的行。我该怎么办 这是我的密码Ios Iphone中的分组表视图,ios,iphone,uitableview,Ios,Iphone,Uitableview,我在应用程序中使用分组表视图。表中有两个部分。第一节有3行,第二节有10多行。当我滚动节时,它会在第二节第5行之后显示第1节的行。我该怎么办 这是我的密码 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Return the number of sections. return 2; } - (NSInteger)tableView:(UITableView *)tableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
switch (section) {
case 0:
return 3;
break;
case 1:
return 8;
break;
default:
break;
}
return 5;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
if (indexPath.section==0)
{
if (indexPath.row==0)
{
UILabel *Name=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 20)];
[Name setText:@"First Name "];
[cell.contentView addSubview:Name];
UITextField *textName=[[UITextField alloc]initWithFrame:CGRectMake(140, 10, 150, 20)];
textName.placeholder=@"Enter First Name";
[textName setBorderStyle:UITextBorderStyleNone];
textName.clearButtonMode = UITextFieldViewModeWhileEditing;
textName.keyboardType=UIKeyboardTypeDefault;
textName.returnKeyType=UIReturnKeyDone;
textName.delegate=self;
[cell.contentView addSubview:textName];
}
if (indexPath.row==1)
{
UILabel *Name=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 110, 20)];
[Name setText:@"Middle Name"];
[cell.contentView addSubview:Name];
UITextField *textName=[[UITextField alloc]initWithFrame:CGRectMake(140, 10, 150, 20)];
textName.placeholder=@"Enter Middle Name";
[textName setBorderStyle:UITextBorderStyleNone];
textName.clearButtonMode = UITextFieldViewModeWhileEditing;
textName.keyboardType=UIKeyboardTypeDefault;
textName.returnKeyType=UIReturnKeyDone;
textName.delegate=self;
[cell.contentView addSubview:textName];
}
if (indexPath.row==2)
{
UILabel *Name=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 20)];
[Name setText:@"Last Name "];
[cell.contentView addSubview:Name];
UITextField *textName=[[UITextField alloc]initWithFrame:CGRectMake(140, 10, 150, 20)];
textName.placeholder=@"Enter Last Name";
[textName setBorderStyle:UITextBorderStyleNone];
textName.clearButtonMode = UITextFieldViewModeWhileEditing;
textName.keyboardType=UIKeyboardTypeDefault;
textName.returnKeyType=UIReturnKeyDone;
textName.delegate=self;
[cell.contentView addSubview:textName];
}
}
if (indexPath.section==1) {
if (indexPath.row==0) {
cell.textLabel.text=@"1";
}
if (indexPath.row==1) {
cell.textLabel.text=@"2";
}
if (indexPath.row==2) {
cell.textLabel.text=@"3";
}
if (indexPath.row==3) {
cell.textLabel.text=@"4";
}
if (indexPath.row==4) {
cell.textLabel.text=@"5";
}
if (indexPath.row==5) {
cell.textLabel.text=@"6";
}
if (indexPath.row==6) {
cell.textLabel.text=@"7";
}
if (indexPath.row==7) {
cell.textLabel.text=@"8";
}
}
return cell;
}
您需要2个单元格标识符。问题是您将使用相同标识符的单元格出列。 粘贴代码,应该可以正常工作:D 附言:我没有解决你的标签问题。每次显示单元格时,都可以分配它们。这是错误的
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
NSInteger returnValue = 5;
switch (section) {
case 0:
{
returnValue = 3;
break;
}
case 1:
{
returnValue = 8;
break;
}
default:
break;
}
return returnValue;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifierSection1 = @"Cell1";
static NSString *CellIdentifierSection2 = @"Cell2";
UITableViewCell *cell;
if (indexPath.section==0)
{
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierSection1];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifierSection1] autorelease];
}
if (indexPath.row==0)
{
UILabel *Name=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 20)];
[Name setText:@"First Name "];
[cell.contentView addSubview:Name];
UITextField *textName=[[UITextField alloc]initWithFrame:CGRectMake(140, 10, 150, 20)];
textName.placeholder=@"Enter First Name";
[textName setBorderStyle:UITextBorderStyleNone];
textName.clearButtonMode = UITextFieldViewModeWhileEditing;
textName.keyboardType=UIKeyboardTypeDefault;
textName.returnKeyType=UIReturnKeyDone;
textName.delegate=self;
[cell.contentView addSubview:textName];
}
if (indexPath.row==1)
{
UILabel *Name=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 110, 20)];
[Name setText:@"Middle Name"];
[cell.contentView addSubview:Name];
UITextField *textName=[[UITextField alloc]initWithFrame:CGRectMake(140, 10, 150, 20)];
textName.placeholder=@"Enter Middle Name";
[textName setBorderStyle:UITextBorderStyleNone];
textName.clearButtonMode = UITextFieldViewModeWhileEditing;
textName.keyboardType=UIKeyboardTypeDefault;
textName.returnKeyType=UIReturnKeyDone;
textName.delegate=self;
[cell.contentView addSubview:textName];
}
if (indexPath.row==2)
{
UILabel *Name=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 20)];
[Name setText:@"Last Name "];
[cell.contentView addSubview:Name];
UITextField *textName=[[UITextField alloc]initWithFrame:CGRectMake(140, 10, 150, 20)];
textName.placeholder=@"Enter Last Name";
[textName setBorderStyle:UITextBorderStyleNone];
textName.clearButtonMode = UITextFieldViewModeWhileEditing;
textName.keyboardType=UIKeyboardTypeDefault;
textName.returnKeyType=UIReturnKeyDone;
textName.delegate=self;
[cell.contentView addSubview:textName];
}
}
if (indexPath.section==1) {
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierSection2];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifierSection2] autorelease];
}
if (indexPath.row==0) {
cell.textLabel.text=@"1";
}
if (indexPath.row==1) {
cell.textLabel.text=@"2";
}
if (indexPath.row==2) {
cell.textLabel.text=@"3";
}
if (indexPath.row==3) {
cell.textLabel.text=@"4";
}
if (indexPath.row==4) {
cell.textLabel.text=@"5";
}
if (indexPath.row==5) {
cell.textLabel.text=@"6";
}
if (indexPath.row==6) {
cell.textLabel.text=@"7";
}
if (indexPath.row==7) {
cell.textLabel.text=@"8";
}
}
return cell;
}
编辑:
您可以为每种类型的单元格使用不同的标识符。因此,如果有两种类型的单元格,则使用两个标识符;如果有一种类型,则使用一个标识符,依此类推。标识符用于将特定类型的单元格出列,如果没有可以重用的单元格,它将只分配一个新的。@Alex我很想更好地理解这一点。为什么这种情况需要两个标识符?您是否总是需要每个部分的标识符?谢谢