0

I have in my tableView, a BarButtonItem that shows me this Alert Controller:

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

Now, I would like to say that when I press "OK" in the Alert Controller, the text of one of the textfields is written in the first cell of the tableview. Next, if I enter other data I will have to appear in the second cell of the tableview and so on.

According to you, how should I do it? Do I have to modify this part?

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

Thanks!

  • add the code of cell creation. Then I can add answer clearly – Subramanian P Jul 08 '17 at 15:10
  • @Subramanian I have no code for cell creation, because I could not create it. Cells must be created when I hit the 'OK' button, for now I only have an empty dynamic tableView –  Jul 08 '17 at 15:12

1 Answers1

0

Create a model class to save the data entered by user in alert controller's textField.

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

Once done, create a NSMutableArray to hold the multiple feed info entered by user. In your TableViewController write

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

Now modify the OK action of your alert controller to create a entry in 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];
    }]];

Once you add the entry to array reload the tableView using [self.tableView reloadData]; as shown above.

Implement the delegates and datasource of UITableView as below.

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

Use the FeedArray you created as the tableView data source. In cellForRowAt index path access the corresponding FeedObject and populate the fields of UITableViewCell.

You can either use default cell or use custom cell of your own. Thats all u need. If you have any doubt as to how to load the custom cell u can refer How to load custom cell ( xib) in UICollectionView cell using swift

Sandeep Bhandari
  • 19,999
  • 5
  • 45
  • 78