Objc - 用于向TableView添加单元格的UIAlertController按钮

时间:2017-07-08 14:50:45

标签: ios objective-c uitableview

我在tableView中有一个BarButtonItem,它向我展示了这个警报控制器:

    UIAlertController * alertController = [UIAlertController alertControllerWithTitle: @"Feed RSS: info?"
                                                                          message: @""
                                                                   preferredStyle:UIAlertControllerStyleAlert];

[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
    textField.placeholder = @"Feed URL";
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    textField.borderStyle = UITextBorderStyleRoundedRect;
}];

[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
    textField.placeholder = @"Feed Title";
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    textField.borderStyle = UITextBorderStyleRoundedRect;
}];

[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
    textField.placeholder = @"Feed category";
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    textField.borderStyle = UITextBorderStyleRoundedRect;

}];
[alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

    NSArray * textfields = alertController.textFields;

    UITextField * urlfield = textfields[0];
    UITextField * titlefield = textfields[1];
    UITextField * categoryfield = textfields[2];

}]];

[self presentViewController:alertController animated:YES completion:nil];

现在,我想说当我按下" OK"在警报控制器中,其中一个文本字段的文本写在tableview的第一个单元格中。接下来,如果我输入其他数据,我将不得不出现在tableview的第二个单元格中,依此类推。

根据你的说法,我应该怎么做? 我是否必须修改此部分?

[alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

谢谢!

1 个答案:

答案 0 :(得分:1)

创建一个模型类,将用户输入的数据保存在警报控制器的textField中。

@interface FeedInfo : NSObject
    @property (nonatomic,strong) NSString *feedURL;
    @property (nonatomic,strong) NSString *feedTitle;
    @property (nonatomic,strong) NSString *feedCategory;
@end

完成后,创建一个NSMutableArray来保存用户输入的多个Feed信息。在你的TableViewController中写

@interface ViewController ()
@property(nonatomic,strong) NSMutableArray *feedArray;
@end

现在修改警报控制器的OK操作以在FeedArray中创建条目。

[alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

        NSArray * textfields = alertController.textFields;

        UITextField * urlfield = textfields[0];
        UITextField * titlefield = textfields[1];
        UITextField * categoryfield = textfields[2];

        FeedInfo *newFeed = [[FeedInfo alloc] init];
        newFeed.feedURL = urlfield.text;
        newFeed.feedTitle = titlefield.text;
        newFeed.feedCategory = categoryfield.text;


        [self.feedArray addObject:newFeed];
        [self.tableView reloadData];
    }]];

将数据条目添加到数组后,使用[self.tableView reloadData];重新加载tableView,如上所示。

实现UITableView的委托和数据源,如下所示。

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.feedArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YourCellSubclass *cell = [self.tableView dequeueReusableCellWithIdentifier:@"your_cell_identifier"];
    FeedInfo *feedToShow = [self.feedArray objectAtIndex:indexPath.row];
    cell.feedTitle.text = feedToShow.feedTitle;
    return cell;
}

使用您创建的FeedArray作为tableView数据源。在cellForRowAt索引路径中访问相应的FeedObject并填充UITableViewCell的字段。

您可以使用默认单元格或使用自己的自定义单元格。多数民众赞成你需要的。如果您对如何加载自定义单元格有任何疑问,请参考 How to load custom cell ( xib) in UICollectionView cell using swift