-1

to the point, i have custom cells, inside it has 2 label and 1 textfield. both label and textfield got input from user. i also have other view that has uitableview inside it. my question is how do i populate cell in uitableview? please help.

this is my code inside tableviewcontroller.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1; // i want to populate this using 'count' but i dont know how.
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:[CustomCell reuseIdentifier]];
    if (cell == nil)
    {
        [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
        cell = _customCell;
        _customCell = nil;
    }       
    cell.titleLabel.text = [NSString stringWithFormat:@"%@",titleTextString];
    cell.timerLabel.text = [NSString stringWithFormat:@"%@",timerString];
    cell.statusLabel.text = [NSString stringWithFormat:@"%@",statusString];

    return cell;    
}

how do i populate my tableview if i push add button after finishing input by user? Please if you dont mind help me with code. i'm beginner and im hard to understand by using opinion.

Alan Haggai Alavi
  • 72,802
  • 19
  • 102
  • 127
SyntaxError
  • 161
  • 1
  • 2
  • 9

1 Answers1

1

If I understood your question correctly, you did a custom nib file for your cells that has 2 UILabel in it and one UITextField, and you want to access these objects when populating your table. Here are some steps for this issue:

First, you have to give a tag number for each object in your custom cell. You find this property in the Attribute Inspector in Interface Builder. Say you gave the first label tag 1, the second label 2 and the text field 3.

Second you have to give a. Identifier for this nib file, for example MyCustomCellIdentifier. This identifier will be used later on in the view that has the table so you can link to it.

Third, also in the custom cell nib, you click on the yellow square that says File's Owner and in the Identity Inspector you change the Class to the class name that has the table that will use this custom cell.

Fourth, in the class that you have the table that will use the custom cell, create an outlet of type UITableViewCell. We will link this in the custom nib cell.

Fifth, goto the custom nib cell, click on the cell window, then in the Connections Inspector link New Referencing Outlet to the File's Owner, you will see the outlet that you created in the table class showing here, simply link to it.

Now since the connections are established thing are more easy, in the cellForRowAtIndexPath function (in the class that contains the table for sure), you have to load the custom cell from the nib file as follows:

static NSString *tableIdentifier = @"MyCustomCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableIdentifier];
if(cell == nil)
{
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TheNibClassNameOfYourCustomCell" owner:self options:nil];
    if([nib count] > 0) cell = theNameOfTheOutletYouUsed;
    else NSLog(@"Failed to load from nib file.");
}

Ok, your custom cell is loaded in variable cell, now you have to access every object in it from the tags you created:

UILabel *label1 = (UILabel *)[cell viewWithTag:1];
UILabel *label2 = (UILabel *)[cell viewWithTag:2];
UITextField *textField1 = (UITextField *)[cell viewWithTag:3];

Now you can access everything through label1, label2, and textField1 easily like label1.text = @"Hi";

I hope this answers your question.

antf
  • 3,162
  • 2
  • 26
  • 33
  • thank you for your answer but what should i write inside numberOfRowsInSection? i mean, how do i populate my cell in tableview if i push "add" button. – SyntaxError Jul 29 '12 at 10:33
  • Usually you will have a `NSMutableArray` where you will be pushing all your new data, so every time you click the add button you will add the new data in that array. Therefore you can have the size of the array normally to be used in `numberOfRowsInSection` which will be in that case `[yourArray count];` Also please remember that if your row data are mixed things you might use `NSMutableDictionary` instead. – antf Jul 29 '12 at 10:42
  • um... im sorry, what should i write inside cellForRowAtIndexPath? i put my array inside numberOfRowsInSection using return [array count]; but what should i add inside that cellForRowAtIndexPath? thank you. – SyntaxError Jul 29 '12 at 11:41
  • Can you please clarify your question more? The code above will be put int he `cellForRowAtIndexPath`. Usually you format your cells in this function. If you are storing your data in an array or a dictionary than in this function you will retrieve the text from the array or dictionary and put it in the cells. You will use `NSUInteger row = [indexPath row];` so you can get the correct array index for the correct row index like `[yourArray objectAtIndex:row];` – antf Jul 29 '12 at 11:56