简体   繁体   中英

How to save data from a textfield to a tableview?

I'm working on my first app and I'm kind of new to Objective-C, I want to make it so when someone types in a text field then presses a button it will save it to a table view . Does anyone know how to do this or know any tutorials.

all you have to do is everytime you press the button you'll refresh/update your tableview.

- (IBAction)buttonPressed:(id)sender {
NSString *textNameToAdd = yourTextField.text;
[containerArrayForTableView addObject:textNameToAdd];
[myTableView reloadData];
}

// UITABLEVIEW DELEGATES
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
 }

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
// Return the number of rows in the section.
// Usually the number of items in your array (the one that holds your list)
return [containerArrayForTableView count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//Where we configure the cell in each row

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell;

cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell... setting the text of our cell's label
cell.textLabel.text = [containerArrayForTableView objectAtIndex:indexPath.row];
return cell;
}

refer here if you are having trouble configuring with UITableView.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM