我知道这个问题已经有很多答案,但没有解决我的问题。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
NSString *CellIdentifier = @"Cell";//[NSString st ringWithFormat:@"S%1dR%1d",indexPath.section,indexPath.row];
//UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UITextField *textField_Name, *textField_PhoneNumber;
UIButton *button_AddToMyCon;
if(cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[cell setBackgroundColor:[UIColor clearColor]];
if (indexPath.row < rowcount-2) {
textField_Name = [[UITextField alloc]init];
textField_PhoneNumber = [[UITextField alloc]init];
textField_Name.frame = CGRectMake(10, (cell.frame.size.height-20)/2, 143, 30);
textField_PhoneNumber.frame = CGRectMake(167, (cell.frame.size.height-20)/2, 143, 30);
textField_Name.delegate = self;
textField_PhoneNumber.delegate = self;
textField_Name.borderStyle = UITextBorderStyleRoundedRect;
textField_PhoneNumber.borderStyle = UITextBorderStyleRoundedRect;
textField_Name.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"Link Name" attributes:@{NSForegroundColorAttributeName: [UIColor lightGrayColor]}];
textField_PhoneNumber.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"Web Link / URL" attributes:@{NSForegroundColorAttributeName: [UIColor lightGrayColor]}];
textField_Name.layer.cornerRadius = 5.0;
textField_Name.clipsToBounds = YES;
[textField_Name.layer setBorderColor:[UIColor lightGrayColor].CGColor];
[textField_Name.layer setBorderWidth:0.5];
textField_Name.font = [UIFont fontWithName:@"arial" size:15.0];
textField_PhoneNumber.layer.cornerRadius = 5.0;
textField_PhoneNumber.clipsToBounds = YES;
[textField_PhoneNumber.layer setBorderColor:[UIColor lightGrayColor].CGColor];
[textField_PhoneNumber.layer setBorderWidth:0.5];
textField_PhoneNumber.font = [UIFont fontWithName:@"arial" size:15.0];
textField_Name.textColor = [UIColor colorWithRed:(152.0f/255.0f) green:(152.0f/255.0f) blue:(152.0f/255.0f) alpha:1.0f];
textField_PhoneNumber.textColor = [UIColor colorWithRed:(152.0f/255.0f) green:(152.0f/255.0f) blue:(152.0f/255.0f) alpha:1.0f];
// textField_Name.tag=501+indexPath.row;
textField_Name.tag = ktagName;
NSLog(@"textField_Name.tag %ld",(long)textField_Name.tag);
//textField_PhoneNumber.tag=701+indexPath.row;
textField_PhoneNumber.tag =ktagPhn;
NSLog(@"textField_PhoneNumber.tag %ld",(long)textField_PhoneNumber.tag);
if(intEditSave == 1)
{
[textField_Name setUserInteractionEnabled:NO];
[textField_PhoneNumber setUserInteractionEnabled:NO];
}
else
{
[textField_Name setUserInteractionEnabled:YES];
[textField_PhoneNumber setUserInteractionEnabled:YES];
}
textField_PhoneNumber.keyboardType = UIKeyboardTypeURL;
[cell addSubview:textField_Name];
[cell addSubview:textField_PhoneNumber];
UILabel *linelable=[[UILabel alloc]initWithFrame:CGRectMake(0, cell.contentView.frame.size.height+10, 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]];
}
else if (indexPath.row==rowcount-2){
NSLog(@"indexPath.row==rowcount-1");
UIButton* 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 setBackgroundImage:[UIImage imageNamed:@"addMore_button.png"] forState:UIControlStateNormal];
[cell.contentView addSubview:button_AddMore];
button_AddMore.tag=indexPath.row;
}
else if (indexPath.row==rowcount-1)
{
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 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.contentView addSubview:button_AddToMyCon];
UILabel *linelable=[[UILabel alloc]initWithFrame:CGRectMake(0, cell.contentView.frame.size.height+10, cell.contentView.frame.size.width, 0.5)];
[linelable setBackgroundColor:[UIColor colorWithRed:(152.0f/255.0f) green:(152.0f/255.0f) blue:(152.0f/255.0f) alpha:0.2f]];
}
}
return cell;
}
-(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] insertRowsAtIndexPaths:[NSArray arrayWithObjects:path, nil] withRowAnimation:UITableViewRowAnimationLeft];
NSIndexPath* top = [NSIndexPath indexPathForRow:rowcount-1 inSection:0];
[(UITableView*)[self.view viewWithTag:ktagtableView_detailsEdit] scrollToRowAtIndexPath:top atScrollPosition:UITableViewScrollPositionBottom animated:YES]; //
//[(UITableView *)[self.view viewWithTag:ktagtableView_detailsEdit]reloadData];
}
这里的情况是tableview有4行默认,每行包含两个文本字段。并添加更多按钮和一个保存按钮。点击添加更多按钮,每次添加一行。请上面的guis。
答案 0 :(得分:0)
cell
实例没有初始值。首先在第一行将其设置为nil
。
更合适的方式:
{
NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
// add subviews to cellview
}
// set data to subviews
}