大家好,我是 iOS 新手。我的问题是:
我有一个可变数组和一个表格视图。表视图中的行数的计数是数组的计数。如果我在最后一行输入任何内容,则会创建一个新行。我想要实现的是,如果任何一行留空,则应自动删除该行。提前致谢
@interface SubClassCell : UITableViewCell <UITextFieldDelegate>{
UILabel *lblShowFirst;
UITextField *txtShow;
id<SubclassDelegate> delegate;
NSArray *arrayFirstSection;
NSArray *arraySecondSection;
UILabel *lblShowSecond;
}
@property (nonatomic, retain) UILabel *lblShowFirst;
@property (nonatomic, retain) UILabel *lblShowSecond;
@property (nonatomic, retain) UITextField *txtShow;
@property (nonatomic, assign) id<SubclassDelegate> delegate;
@property (nonatomic, retain) NSArray *arrayFirstSection;
@property (nonatomic, retain) NSArray *arraySecondSection;
-(void) setCellTable:(NSIndexPath *) indexPath;
@end
@protocol SubclassDelegate <NSObject>
-(void)getTxtField:(NSString *)txtValue;
-(void)insertNewRows:(UITextField *)textF;
-(void)removeNewRows:(UITextField *)textF;
@end
subclass.m
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[txtShow resignFirstResponder];
return YES;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSLog(@"%@",textField.text);
if([textField.text length] == 1)
[self.delegate insertNewRows:textField];
else if ([textField.text length]==2){
[self.delegate removeNewRows:textField];
}
//if([textField.text length]==0){
// [self.delegate removeNewRows:textField];
// }
return YES;
}
Main class.m
-(void)insertNewRows:(UITextField *)textF
{
[arrayTagFirst addObject:textF.text];
[tableNewContactFirst beginUpdates];
[tableNewContactFirst insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath ndexPathForRow:[arrayTagFirst count]-1 inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
[tableNewContactFirst endUpdates];
if (textF.editing && textF.text==0) {
[arrayTagFirst removeLastObject];
[tableNewContactFirst beginUpdates];
}
}