0

I am developing an app that can store contacts. In the table view I have the last name showing up but wish to also have the first name and the right justified the date. The first name is saved as fist name and the date field as date. Can any one run me through how to do this?

Thanks

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{


return [[self.fetchedResultsController sections]count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
id <NSFetchedResultsSectionInfo> secInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
return [secInfo numberOfObjects];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

Contacts *contacts = [self.fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = contacts.name;


return cell;
}

-(NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [[[self.fetchedResultsController sections]objectAtIndex:section]name];
}
Scubadivingfool
  • 1,227
  • 2
  • 10
  • 23

2 Answers2

1

Do something like this . . In the "cellForRowAtIndexPath" Method.

- (UITableViewCell *)tableView:(UITableView *)atableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{
    UILabel *firstName;
    UILabel *lastName;

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (!cell)
    {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        firstName =[[UILabel alloc]initWithFrame:CGRectMake(20,0,250,45)] ;
        [cell.contentView addSubview:firstName]; // As your requirement ,adjust the CGRECT. 

        firstName = [UIColor colorWithRed:0.75 green:0.25 blue:0.25 alpha:1.0];
        firstName.backgroundColor = [UIColor clearColor];
        firstName.font = [UIFont systemFontOfSize:[UIFont labelFontSize]];

        lastName =[[UILabel alloc]initWithFrame:CGRectMake(20,35,250,30)] ;
        [cell.contentView addSubview:lastName];

    }
   firstName.text = ['List' objectAtIndex:indexPath.row];
    bottomLabel.text=['List_name' objectAtIndex:indexPath.row];

    return cell;

}
Kumar KL
  • 15,315
  • 9
  • 38
  • 60
  • When I change to this code, I get three errors " Unknown receiver'_tableView', 'Bad receiver type 'int' at the first List, and then multi-character character constant' at the List_name – Scubadivingfool May 11 '13 at 11:21
  • did you synthesise your tableView. ? . " @synthesize tableView=_tableView; "" Or just use "self.tableView" . – Kumar KL May 11 '13 at 11:23
  • and then about first and last name [Lists of your contacts] should be assign properly according to the tableView delegate. – Kumar KL May 11 '13 at 11:26
  • "and then about first and last name [Lists of your contacts] should be assign properly according to the tableView delegate.". Not a clue what you mean here. Sorry still new at this. – Scubadivingfool May 11 '13 at 11:35
  • http://stackoverflow.com/questions/5831813/delegate-and-datasource-methods-for-uitableview?rq=1 . Go through it , You will get the concept of UITableView. – Kumar KL May 11 '13 at 11:43
  • @Scubadivingfool You get error for _tableView probably because your tableView is named something else (possibly tableView, without the underscore). The two last sentences in his answer (`firstName.text= ` and `lastName.text=`) should be changed to what you use to get your data. In your code in your question you created a Contact-object, and used contact.name, just do the same for firstName.text and lastName.text. – Sti May 11 '13 at 12:07
0

You want both first name AND last name on the main label, and a date on another? Or all three on the same label? Or in three different as in suggested answer?

This might be the easiest way to achieve something like you're requesting:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (!cell)//Setting CellStyle to Subtitle, might have to change that in Storyboard as well, by clicking the cell and choose Subtitle as style
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

    Contacts *contacts = [self.fetchedResultsController objectAtIndexPath:indexPath];

    //If I understood right, you knew how to get firstName, and lastName(through .name?), but not both at the same time into the same label?
    //Switch out the following 'contacts.firstName' and 'contacts.name' to fit your needs if it's not right:
    NSString *fullName = [NSString stringWithFormat:@"%@ %@", contacts.firstName, contacts.name];
    cell.textLabel.text = fullName;
    //And put the date on the 'subtitle'(detailTextLabel). Might not work if the date-variable isn't a text, but another type of object, like NSDate etc.
    cell.detailTextLabel.text = contacts.date;

    return cell;

}

If your contacts.date-variable turns out to be an NSDate, take a look at this link to put the date in an NSString, and then use cell.detailTextLabel.text = stringFromDate;

Community
  • 1
  • 1
Sti
  • 8,275
  • 9
  • 62
  • 124