Reputation: 999
After quite a long time perusing the web i have decided to come here as i usually have my questions answered super speedy and super well on Stack Overflow!
I am attempting to complete the following and would be grateful of any suggestions or pointers in the right direction:
Any suggestions regarding this would be greatly appreciated
Thanks!
Tom
Upvotes: 1
Views: 2977
Reputation: 686
UPDATE FOR SWIFT 4
Instead of reloading the whole table you can use beginUpdate and endUpdate here.
Add target to the cell button(You can use view tag or create new TableviewCell class).
cell.addBtn.tag = indexPath.row
cell.addBtn.addTarget(self, action: #selector(self.addTextField(_:)), for: UIControlEvents.touchUpInside)
cell.txtField.tag = indexPath.row
Then implement this method:
@objc func addTextField(_ sender:UIButton) {
let indexPath = IndexPath(row: sender.tag, section: 0)
if let cell = tblView.cellForRow(at: indexPath) as? TableCell {
tblArray.append(cell.txtField.text!)
}
let newRow = IndexPath(row: sender.tag + 1, section: 0)
tblView.beginUpdates()
tblView.insertRows(at: [newRow], with: .left)
tblView.endUpdates()
}
Do not forget to put var tblArray:[Any] = [""]
at start.
Hope this anyone who is looking for Swift answer.
Happy coding.
Upvotes: 0
Reputation: 29967
This pseudocode might be a starting point:
// define an NSMutableArray called fields
- (IBAction)addField:(id)sender {
// create a new text field here and add it to the array
// then reload data
}
- (IBAction)save:(id)sender {
for(UITextField *textField in fields) {
// whatever you have to do
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// you have as many rows as textfields
return [fields count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell...
// For row N, you add the textfield N to the array.
[cell addSubview:(UITextField *)[fields objectAtIndex:indexPath.row]];
}
Upvotes: 1