How to insert items to a UITableView when a UIButton is clicked in iOS

前端 未结 2 444
甜味超标
甜味超标 2021-01-14 06:28

I have been practicing with tableViews but I cannot figure out how to insert new items when a button is clicked.

This is what I have:

BIDViewControlle

相关标签:
2条回答
  • 2021-01-14 07:23

    You're on the right track. Make sure you're setting your delegate and data source correctly and your outlet is wired to the button.

    Edit: Also fairly certain you can't just @"blah" strings into a mutable array. Try initializing the text as an NSString first, and then pass it in as a pointer.

    0 讨论(0)
  • 2021-01-14 07:33

    You are doing it right,

    - (IBAction)addItems:(id)sender {
        // I thought it was as simple as this, but it doesn't work
        [self.names addObject:@"Brad"];
        [self.tableView relaodData];
    }
    

    is the correct way.

    Please double check the variable tableView, I can't see the declaration of it.

    make sure you have,

    @property(weak, nonatomic) IBOutlet UITableView *tableView;
    [self.tableView setDelegate:self];
    [self.tableView setDataSource:self];
    

    and properly connected the tableView to .nib

    0 讨论(0)
提交回复
热议问题