-2

皆さんこんにちは、私は iOS 初心者です。私の質問は:

Mutable 配列とテーブルビューがあります。テーブル ビューの行数のカウントは、配列のカウントです。最後の行に何か入力すると、新しい行が作成されます。私が達成したいのは、行のいずれかが空白のままになっている場合、その行は自動的に削除されることです。前もって感謝します

@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 に答える