0

我正在开发一个可以存储联系人的应用程序。在表格视图中,我显示了姓氏,但希望也有名字和右对齐的日期。名字保存为名字,日期字段保存为日期。谁能告诉我如何做到这一点?

谢谢

 - (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];
}
4

2 回答 2

1

做这样的事情。. 在“ cellForRowAtIndexPath”方法中。

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

}
于 2013-05-11T04:42:25.033 回答
0

您想在主标签上同时显示名字和姓氏,而在另一个标签上显示日期?还是三个都在同一个标​​签上?还是建议答案中的三个不同?

这可能是实现您所要求的最简单的方法:

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

}

如果您的 contacts.date-variable 原来是 NSDate,请查看此链接以将日期放入 NSString,然后使用cell.detailTextLabel.text = stringFromDate;

于 2013-05-11T12:24:16.793 回答