1

I have a tableview cell. There's a button and a label in it. When the user clicks on the button, i want to popup an message box where the user will be able to type some text and submit. At that instance (When the user type the text and submits) the text typed by the user should be displayed on the UILabel(In that particular cell) of that cell. How can i do this ?

cell.cellButton.tag = indexPath.row;
        [cell.cellButton addTarget:self action:@selector(cellButtonClicked:) forControlEvents:UIControlEventTouchUpInside];



-(void)cellButtonClicked: (id)sender {

    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"alert" message:@"Enter your text" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok"];
    alert.alertViewStyle = UIAlertViewStylePlainTextInput;
    [alert show];

}

Note: This is how each cell looks like enter image description here

QUestions :

1.) How to populate the label in the Cell with the text ? (Please consider that i already have the text with me)

Illep
  • 16,375
  • 46
  • 171
  • 302
  • 1
    This is too broad. What part of this do you need help with? Do you know how to get the text from the alert view? Do you know how to update your data model with the text? Do you know how to reload a table cell? – rmaddy Aug 05 '15 at 04:33
  • I would create a subclass of UIAlertView, and UIAlertViewDelegate Category which tells me when user edit some text and submitted. – antonio081014 Aug 05 '15 at 04:46
  • @antonio081014 Why? There's no need. `UIAlertView` already has what is needed. – rmaddy Aug 05 '15 at 04:47
  • 1
    @Illep Do you know how to put text in cells under normal conditions. This is no different. Put the new text in your data model and reload the row. – rmaddy Aug 05 '15 at 04:48
  • @rmaddy but how could the UIAlertView pass the text to Cell, or UITableViewController, I suppose? – antonio081014 Aug 05 '15 at 04:48
  • @antonio081014 The view controller is the alert view's delegate. – rmaddy Aug 05 '15 at 04:49
  • OK, I might misunderstood the problem. The other way you could just get the Cell, and update the text, while update your model. – antonio081014 Aug 05 '15 at 04:50

3 Answers3

1

Set the tag for alert view in button action as

-(void)cellButtonClicked: (id)sender {
    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"alert" message:@"Enter your text" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
    alert.alertViewStyle = UIAlertViewStylePlainTextInput;
    alert.tag = sender.tag;
    [alert show];
}

After that write the below code in alert view delegate as

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 1) {
        NSString *name = [alertView textFieldAtIndex:0].text;
        // Insert whatever needs to be done with "name"
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:alertView.tag inSection:0];

        UITableViewCell *cell = [_myTableview cellForRowAtIndexPath:indexPath];
        for (UIView *view in cell.contentView.subviews) {
            if([view isKindOfClass:[UILabel class]]){
                UILabel *label = (UILabel*) view;
                label.text = name;
            }
        }
    }
} 

Hope it will help you.

Vishnuvardhan
  • 5,081
  • 1
  • 17
  • 33
  • 1) Why dig into the cell's subviews and update every label? Surely the cell class has a property for the label. 2) This solution will fail as soon as the user scrolls the table view. You need to update the data model. – rmaddy Aug 05 '15 at 05:01
  • I have heard that apple rejects UIAlertViews with textboxes in it. I want to know if this is true ? – Illep Aug 05 '15 at 05:58
  • Sorry,I am not much more aware of rejection thing.Check this link it may help you. http://stackoverflow.com/questions/3634204/about-uialertview-with-textfield – Vishnuvardhan Aug 05 '15 at 06:10
0

You try this code

-(void)cellButtonClicked: (id)sender {

    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"alert" message:@"Enter your text" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok"];
    alert.alertViewStyle = UIAlertViewStylePlainTextInput;
    alert.tag = sender.tag;
    [alert show];

}

Alert View Ok Button click code

You get TextField text and set as cell label using indexPath

-(void)alertView:(UIAlertView *)alertView1 clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex==alertView1.firstOtherButtonIndex) {
        NSIndexPath *theIndexPath=[NSIndexPath indexPathForRow:alertView1.tag inSection:0];
        UITableViewCell *cell = [self.yourTableViewName cellForRowAtIndexPath:theIndexPath];
        cell.yourLabel.text = [alertView1 textFieldAtIndex:0];
        [self.tableView reloadRowsAtIndexPaths:theIndexPath withRowAnimation:UITableViewRowAnimationNone];

    }
}
Dharmesh Dhorajiya
  • 3,976
  • 9
  • 30
  • 39
  • 1
    I guess it's better to mention also update the data model here if necessary. – antonio081014 Aug 05 '15 at 04:52
  • There is no reason to get the cell and update its text directly. In fact, that's a bad approach. Simply update the data model and call `reloadRowsAtIndexPath`. Let the `cellForRowAtIndexPath` method set the cell's text as needed from the updated data model. – rmaddy Aug 05 '15 at 04:54
  • And the `if` statement should be `if (buttonIndex == alertView1.firstOtherButtonIndex)`. – rmaddy Aug 05 '15 at 04:56
  • My suggestion is less error prone. It's also more clear which button you are checking against. In your code, what does the `0` represent? Is it the Cancel button or the OK button? – rmaddy Aug 05 '15 at 05:29
0

The best practice to implement the table view is to have a proper data source.

So if all of your cells are picking up the text from a common place, let's say an mutable array, then you can update the text on that particular indexpath.row in that array and reload the table.

Vikas Dadheech
  • 1,672
  • 12
  • 23