Add a UITextField to a UITableView on a button click

Add a UITextField to a UITableView on a button click



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




2 Answers
2



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






Thanks guys.. i know that was a little vague ... but his has really helped and i am well on my way. Thank you

– Tom G
Dec 16 '09 at 12:22






I feel like you'd rather calling cell addSubview inside the if (cell==nil) block so you don't add it every time the cell gets reused.

– drewish
Jul 27 '12 at 16:40



cell addSubview


if (cell==nil)



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.


var tblArray:[Any] = [""]



Hope this anyone who is looking for Swift answer.



Happy coding.



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế