【问题标题】:Objc - UIAlertController buttons to add cells to a TableViewObjc - UIAlertController 按钮将单元格添加到 TableView
【发布时间】:2017-12-12 17:36:49
【问题描述】:

我的 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];

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

按照你的说法,我该怎么做? 我需要修改这部分吗?

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

谢谢!

【问题讨论】:

  • 添加单元格创建代码。然后我可以清楚地添加答案
  • @Subramanian 我没有用于创建单元格的代码,因为我无法创建它。当我点击“确定”按钮时必须创建单元格,现在我只有一个空的动态 tableView

标签: ios objective-c uitableview


【解决方案1】:

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

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

完成后,创建一个NSMutableArray 来保存用户输入的多个提要信息。在你的 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的delegate和datasource。

-(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

【讨论】:

  • cell.feedTitle.text = feedToShow.feedTitle;不工作! :(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-05-10
  • 2020-10-20
  • 2015-09-17
  • 1970-01-01
  • 2015-04-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多