deMasta
deMasta

Reputation: 1

Adding to the UITableView on UIButton

At one ViewController is located separately UITableView, UITextField and UIButton. I need to add UITextField's text to the cell of the table by button. I set the flag on the pressing process:

- (IBAction)addingButon:(id)sender {

  _addingFlag = true; 

}

And then in the method of the table do the following:

- (UITableViewCell *)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString  *cellID = @"dataCell";

UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:cellID];

while (true) {

    if (_addingFlag && _textBox.text.length != 0) {

        cell.textLabel.text = _textBox.text;

        [tableView reloadData];
    }

    _addingFlag = false;

    break;

    return cell; 

 }

Data in table appears only when scrolling and in the strange order. Can you please tell how to fix it. Working with Objective-C just a couple of days.

Upvotes: 0

Views: 71

Answers (1)

Jed Fox
Jed Fox

Reputation: 3025

tableView:cellForRowAtIndexPath: should return immediately. Instead, call [tableView reloadData] in addingButton::

- (IBAction)addingButon:(id)sender {
    [self.tableView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString  *cellID = @"dataCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

    cell.textLabel.text = _textBox.text;
    return cell; 
 }

Upvotes: 2

Related Questions