在点击索引路径n-3处的按钮时动态地添加行。在我的表视图中,默认情况下总共有6行,4个包含文本字段和另外两个包含按钮(添加更多和保存)。在最后两行以上的位置添加按钮。
-(void)addMoreButton:(UIButton *)sender
{
rowCount=rowCount+1;
int section = 0;
long row = rowCount;
NSIndexPath* path = [NSIndexPath indexPathForRow:row-3 inSection:section];
[(UITableView *)[self.view viewWithTag:ktagtableView_detailsEdit] beginUpdates];
[(UITableView *)[self.view viewWithTag:ktagtableView_detailsEdit] insertRowsAtIndexPaths:[NSArray arrayWithObjects:path, nil] withRowAnimation:UITableViewRowAnimationLeft];
[(UITableView *)[self.view viewWithTag:ktagtableView_detailsEdit] endUpdates];
NSIndexPath* top = [NSIndexPath indexPathForRow:rowCount-1 inSection:0];
[(UITableView*)[self.view viewWithTag:ktagtableView_detailsEdit] scrollToRowAtIndexPath:top atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}当rowCount大于8时,调用scrollToRowAtIndexPath方法,然后调用cellForRowAtIndexPath方法两次,就会出现问题。第一次由传递索引n-3的insertRowAtIndex调用,第二次调用scrollToRowAtIndexPath时,它传递索引n-1,当
if (indexPath.row == rowCount-1) // This is index for last row that contains save button and this condition match thus instead of adding textfields it starts adding save button.
if (indexPath.row == rowCount-2) // This is Add more button.
if (indexPath.row < rowCount-2) // This is for adding textFields.请为以上内容提供指导。如果有任何疑问,请随时询问。前两天我被困在里面了。
更新: cellForRowAtIndexPath方法。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier;
CellIdentifier = [NSString stringWithFormat:@"S%1dR%1d",indexPath.section,indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];
CellIdentifier = [NSString stringWithFormat:@"S%1dR%1d",indexPath.section,indexPath.row];
UITextField *textField_SocialProfile;
UIButton *button_AddMore, *button_AddToMyCon;
UIImageView *imageView_Add;
if(cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[cell setBackgroundColor:[UIColor clearColor]];
linelable=[[UILabel alloc]initWithFrame:CGRectMake(0, cell.contentView.frame.size.height+9, cell.contentView.frame.size.width, 0.5)];
[cell.contentView addSubview:linelable];
[linelable setBackgroundColor:[UIColor colorWithRed:(152.0f/255.0f) green:(152.0f/255.0f) blue:(152.0f/255.0f) alpha:0.2f]];
if (indexPath.row < rowCount-2)
{
textField_SocialProfile = [[UITextField alloc]init];
textField_SocialProfile.tag=indexPath.row+501;
NSLog(@"textField_SocialProfile.tag %ld",(long)textField_SocialProfile.tag);
textField_SocialProfile.frame = CGRectMake(70, (tableView.rowHeight - 20)/2, 230, 30);
textField_SocialProfile.delegate = self;
textField_SocialProfile.borderStyle = UITextBorderStyleRoundedRect;
switch (indexPath.row) {
case 0:
textField_SocialProfile.attributedPlaceholder = [[NSAttributedString alloc] initWithString:ktagInstagram attributes:@{NSForegroundColorAttributeName: [UIColor lightGrayColor]}];
break;
case 1:
textField_SocialProfile.attributedPlaceholder = [[NSAttributedString alloc] initWithString:ktagFacebook attributes:@{NSForegroundColorAttributeName: [UIColor lightGrayColor]}];
break;
case 2:
textField_SocialProfile.attributedPlaceholder = [[NSAttributedString alloc] initWithString:ktagTwitter attributes:@{NSForegroundColorAttributeName: [UIColor lightGrayColor]}];
break;
case 3:
textField_SocialProfile.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"Linkedin" attributes:@{NSForegroundColorAttributeName: [UIColor lightGrayColor]}];
break;
default:
textField_SocialProfile.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"Social Networking" attributes:@{NSForegroundColorAttributeName: [UIColor lightGrayColor]}];
}
textField_SocialProfile.textColor = [UIColor colorWithRed:(152.0f/255.0f) green:(152.0f/255.0f) blue:(152.0f/255.0f) alpha:1.0f];
textField_SocialProfile.layer.cornerRadius = 5.0;
textField_SocialProfile.clipsToBounds = YES;
[textField_SocialProfile.layer setBorderColor:[UIColor lightGrayColor].CGColor];
[textField_SocialProfile.layer setBorderWidth:0.5];
if (intEditSave==1) {
[textField_SocialProfile setUserInteractionEnabled:NO];
}
else
{
[textField_SocialProfile setUserInteractionEnabled:YES];
}
imageView_Add = [[UIImageView alloc]init];
switch (indexPath.row) {
case 0:
imageView_Add.image = [UIImage imageNamed:@"instagramIcon.png"];
textField_SocialProfile.text=@"www.instagram.com/";
if (dataSourceArray.count > indexPath.row) {
textField_SocialProfile.text = [[dataSourceArray objectAtIndex:indexPath.row] objectForKey:@"SocialProfile"];
if ([[NSString stringWithFormat:@"%@",[[dataSourceArray objectAtIndex:indexPath.row] objectForKey:@"SocialProfile"]] length] != 0) {
textField_SocialProfile.text=[[dataSourceArray objectAtIndex:indexPath.row] objectForKey:@"SocialProfile"];
if([textField_SocialProfile.text isEqualToString:@""])
{
textField_SocialProfile.text=[NSString stringWithFormat:@"www.instagram.com/%@",[[dataSourceArray objectAtIndex:indexPath.row] objectForKey:@"SocialProfile"]];
}
}else{
textField_SocialProfile.text=@"www.instagram.com/";
}
}
break;
case 1:
imageView_Add.image = [UIImage imageNamed:@"facebokIcon.png"];
textField_SocialProfile.text=@"www.facebook.com/";
if (dataSourceArray.count > indexPath.row) {
textField_SocialProfile.text = [[dataSourceArray objectAtIndex:indexPath.row] objectForKey:@"SocialProfile"];
if ([[NSString stringWithFormat:@"%@",[[dataSourceArray objectAtIndex:indexPath.row] objectForKey:@"SocialProfile"]] length] != 0) {
textField_SocialProfile.text=[[dataSourceArray objectAtIndex:indexPath.row] objectForKey:@"SocialProfile"];
if([textField_SocialProfile.text isEqualToString:@""])
{
textField_SocialProfile.text=[NSString stringWithFormat:@"www.facebook.com/%@",[[dataSourceArray objectAtIndex:indexPath.row] objectForKey:@"SocialProfile"]];
}
}else{
textField_SocialProfile.text=@"www.facebook.com/";
}
} break;
case 2:
imageView_Add.image = [UIImage imageNamed:@"twitterIcon.png"];
textField_SocialProfile.text=@"www.twitter.com/";
if (dataSourceArray.count > indexPath.row) {
textField_SocialProfile.text = [[dataSourceArray objectAtIndex:indexPath.row] objectForKey:@"SocialProfile"];
if ([[NSString stringWithFormat:@"%@",[[dataSourceArray objectAtIndex:indexPath.row] objectForKey:@"SocialProfile"]] length] != 0) {
textField_SocialProfile.text=[[dataSourceArray objectAtIndex:indexPath.row] objectForKey:@"SocialProfile"];
if([textField_SocialProfile.text isEqualToString:@""])
{
textField_SocialProfile.text=[NSString stringWithFormat:@"www.twitter.com/%@",[[dataSourceArray objectAtIndex:indexPath.row] objectForKey:@"SocialProfile"]];
}
}else{
textField_SocialProfile.text=@"www.twitter.com/";
}
} break;
case 3:
imageView_Add.image = [UIImage imageNamed:@"linkedinIcon.png"];
textField_SocialProfile.text=@"www.linkedin.com/";
if (dataSourceArray.count > indexPath.row) {
textField_SocialProfile.text = [[dataSourceArray objectAtIndex:indexPath.row] objectForKey:@"SocialProfile"];
if ([[NSString stringWithFormat:@"%@",[[dataSourceArray objectAtIndex:indexPath.row] objectForKey:@"SocialProfile"]] length] != 0) {
textField_SocialProfile.text=[[dataSourceArray objectAtIndex:indexPath.row] objectForKey:@"SocialProfile"];
if([textField_SocialProfile.text isEqualToString:@""])
{
textField_SocialProfile.text=[NSString stringWithFormat:@"www.linkedin.com/%@",[[dataSourceArray objectAtIndex:indexPath.row] objectForKey:@"SocialProfile"]];
}
}else{
textField_SocialProfile.text=@"www.linkedin.com/";
}
} break;
default:
imageView_Add.image = [UIImage imageNamed:@"myconIcon.png"];
if (dataSourceArray.count > indexPath.row) {
textField_SocialProfile.text = [[dataSourceArray objectAtIndex:indexPath.row] objectForKey:@"SocialProfile"];}
break;
}
NSString *socialName= textField_SocialProfile.text;
switch (indexPath.row) {
case 0:
if ([socialName rangeOfString:@"www.instagram.com/"].location!=NSNotFound) {
imageView_Add.image = [UIImage imageNamed:@"instagramIcon.png"];
}
else if ([socialName rangeOfString:@"www.linkedin.com/"].location!=NSNotFound)
{
imageView_Add.image = [UIImage imageNamed:@"linkedinIcon.png"];
}
else if ([socialName rangeOfString:@"www.facebook.com/"].location!=NSNotFound)
{
imageView_Add.image = [UIImage imageNamed:@"facebokIcon.png"];
}
else if ([socialName rangeOfString:@"www.twitter.com/"].location!=NSNotFound)
{
imageView_Add.image = [UIImage imageNamed:@"twitterIcon.png"];
}
else
{
imageView_Add.image = [UIImage imageNamed:@"myconIcon.png"];
}
break;
case 1:
if ([socialName rangeOfString:@"www.instagram.com/"].location!=NSNotFound) {
imageView_Add.image = [UIImage imageNamed:@"instagramIcon.png"];
}
else if ([socialName rangeOfString:@"www.linkedin.com/"].location!=NSNotFound)
{
imageView_Add.image = [UIImage imageNamed:@"linkedinIcon.png"];
}
else if ([socialName rangeOfString:@"www.facebook.com/"].location!=NSNotFound)
{
imageView_Add.image = [UIImage imageNamed:@"facebokIcon.png"];
}
else if ([socialName rangeOfString:@"www.twitter.com/"].location!=NSNotFound)
{
imageView_Add.image = [UIImage imageNamed:@"twitterIcon.png"];
}
else
{
imageView_Add.image = [UIImage imageNamed:@"myconIcon.png"];
}
break;
case 2:
if ([socialName rangeOfString:@"www.instagram.com/"].location!=NSNotFound) {
imageView_Add.image = [UIImage imageNamed:@"instagramIcon.png"];
}
else if ([socialName rangeOfString:@"www.linkedin.com/"].location!=NSNotFound)
{
imageView_Add.image = [UIImage imageNamed:@"linkedinIcon.png"];
}
else if ([socialName rangeOfString:@"www.facebook.com/"].location!=NSNotFound)
{
imageView_Add.image = [UIImage imageNamed:@"facebokIcon.png"];
}
else if ([socialName rangeOfString:@"www.twitter.com/"].location!=NSNotFound)
{
imageView_Add.image = [UIImage imageNamed:@"twitterIcon.png"];
}
else
{
imageView_Add.image = [UIImage imageNamed:@"myconIcon.png"];
}
break;
case 3:
if ([socialName rangeOfString:@"www.instagram.com/"].location!=NSNotFound) {
imageView_Add.image = [UIImage imageNamed:@"instagramIcon.png"];
}
else if ([socialName rangeOfString:@"www.linkedin.com/"].location!=NSNotFound)
{
imageView_Add.image = [UIImage imageNamed:@"linkedinIcon.png"];
}
else if ([socialName rangeOfString:@"www.facebook.com/"].location!=NSNotFound)
{
imageView_Add.image = [UIImage imageNamed:@"facebokIcon.png"];
}
else if ([socialName rangeOfString:@"www.twitter.com/"].location!=NSNotFound)
{
imageView_Add.image = [UIImage imageNamed:@"twitterIcon.png"];
}
else
{
imageView_Add.image = [UIImage imageNamed:@"myconIcon.png"];
}
break;
default:
if ([socialName rangeOfString:@"www.instagram.com/"].location!=NSNotFound) {
imageView_Add.image = [UIImage imageNamed:@"instagramIcon.png"];
}
else if ([socialName rangeOfString:@"www.linkedin.com/"].location!=NSNotFound)
{
imageView_Add.image = [UIImage imageNamed:@"linkedinIcon.png"];
}
else if ([socialName rangeOfString:@"www.facebook.com/"].location!=NSNotFound)
{
imageView_Add.image = [UIImage imageNamed:@"facebokIcon.png"];
}
else if ([socialName rangeOfString:@"www.twitter.com/"].location!=NSNotFound)
{
imageView_Add.image = [UIImage imageNamed:@"twitterIcon.png"];
}
else
{
imageView_Add.image = [UIImage imageNamed:@"myconIcon.png"];
}
break;
}
imageView_Add.frame = CGRectMake(30, (tableView.rowHeight - 8)/2, 15, 15);
textField_SocialProfile.font = [UIFont fontWithName:@"arial" size:15.0];
[cell addSubview:imageView_Add];
imageView_Add = Nil;
[cell addSubview:textField_SocialProfile];
//textField_SocialProfile = Nil;
}
else if(indexPath.row == rowCount-2)//Add more button
{
// NSLog(@"textf: %@",textF.text);
NSLog(@"AddMore: %ld",indexPath.row);
NSLog(@"AddMore: %d",rowCount);
button_AddMore = [[UIButton alloc]init];
[button_AddMore setTitle:ktagAdd_More forState:UIControlStateNormal];
[button_AddMore titleLabel].font = [UIFont fontWithName:@"arial" size:15.0];
[button_AddMore setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button_AddMore addTarget:self action:@selector(addMoreButton:) forControlEvents:UIControlEventTouchUpInside];
button_AddMore.tag = ktagbutton_AddMore;
button_AddMore.frame = CGRectMake(90, (cell.frame.size.height-20)/2, 140, 30);
button_AddMore.center=CGPointMake(90+70, 22);
[button_AddMore setBackgroundImage:[UIImage imageNamed:@"addMore_button.png"] forState:UIControlStateNormal];
[cell addSubview:button_AddMore];
linelable.hidden=NO;
if(intEditSave == 1)//Editing is complete
{
button_AddMore.enabled=NO;
}
else
{
button_AddMore.enabled=YES;
}
}
else if(indexPath.row == rowCount-1)//Save fields button
{
NSLog(@"Save: %ld",indexPath.row);
NSLog(@"Save: %d",rowCount);
button_AddToMyCon = [[UIButton alloc]init];
[button_AddToMyCon setTitle:ktagAdd_to_MyCon forState:UIControlStateNormal];
[button_AddToMyCon titleLabel].font = [UIFont fontWithName:@"arial" size:15.0];
[button_AddToMyCon setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button_AddToMyCon addTarget:self action:@selector(save_Fields:) forControlEvents:UIControlEventTouchUpInside];
button_AddToMyCon.tag = ktagbutton_AddToMyCon;
button_AddToMyCon.frame = CGRectMake(90, (cell.frame.size.height-20)/2, 140, 30);
button_AddToMyCon.center=CGPointMake(90+70, 22);
[button_AddToMyCon setBackgroundColor:[UIColor colorWithRed:(234.0f/255.0f) green:(134.0f/255.0f) blue:(59.0f/255.0f) alpha:1.0]];
button_AddToMyCon.layer.cornerRadius=4;
[cell addSubview:button_AddToMyCon];
linelable.hidden=YES;
}
}
return cell;
}问题是,如果我将dequeReusable属性设置为nil,那么这个问题就解决了,当前数据开始在滚动时消失。
发布于 2014-12-16 16:38:10
这是tableView数据源和委托方法的一种行为。当scrollToRowAtIndexPath时调用cellForRowAtIndexPath方法,因为当表格滚动显示屏幕上不可见的单元格时,该方法执行重用单元格,因此cellForRowAtIndexPath为新的可见单元格加载内容。
这是默认和正确的行为,您不能绕过它。
现在,您需要使用它来管理应用程序需求。我不这样认为,这种行为对你的需求有任何漏洞。
因为cellForRowAtIndexPath是一个被多次调用的方法,您在这里不能限制任何约束。如果你认为你的预期行为没有实现,请留下评论。
编辑:
在为你创建一个示例演示的同时,我想出了一点--它可能会很快解决你的问题。
在下面的代码行中,动画无法正常工作:
[(UITableView *)[self.view viewWithTag:ktagtableView_detailsEdit] insertRowsAtIndexPaths:[NSArray arrayWithObjects:path, nil] withRowAnimation:UITableViewRowAnimationLeft];将行动画UITableViewRowAnimationLeft改为UITableViewRowAnimationAutomatic,请参见下面的更新行:
[(UITableView *)[self.view viewWithTag:ktagtableView_detailsEdit] insertRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationAutomatic];不过,如果这不能解决您的问题,您可以在这里提供示例演示。
为了简单起见,我使用了限制控制,并将重点放在功能上。
Dynamic Cell Creation Demo - Click Here

https://stackoverflow.com/questions/27499484
复制相似问题