2013-05-11 22 views
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]; 
} 
+1

怎么回事? – 2013-05-11 02:02:56

+0

什么问题是我不知道如何做到这一点。我试图在名称文本字段后添加“cell.textLabel.text = contacts.firstname”,但只是名字而不是两个。 – Scubadivingfool 2013-05-11 02:06:38

+0

你在使用StoryBoards吗? – 2013-05-11 02:08:16

回答

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; 

} 
+0

当我更改为此代码时,出现三个错误“Unknown receiver'_tableView','Bad receiver type'int'在第一个List,然后是多字符字符常量'在List_name – Scubadivingfool 2013-05-11 11:21:02

+0

你合成了你的tableView。 ? 。 “@synthesize tableView = _tableView;”“或者只是使用”self.tableView“ – 2013-05-11 11:23:44

+0

然后关于名和姓[你的联系人列表]应该根据tableView委托正确分配 – 2013-05-11 11:26:42

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变量原来是一个NSDate,看看this link把日期一个NSString,然后用cell.detailTextLabel.text = stringFromDate;

相关问题