-2

大家好,我是 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];
    }


}
4

1 回答 1

1

这就是你如何删除 UITableView 中的行

[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];

你还需要从 NSMutableArray 中删除它

[yourMutableArray removeObjectAtIndex:indexPath];

要获取空单元格的 indexPath,您需要添加一些代码以便我弄清楚。似乎您在每个单元格中添加了一个 UITextField,请告诉我相同的信息,以便我可以帮助您。希望能帮助到你。快乐编码:)

于 2012-09-17T11:38:43.710 回答