我正在开发一个可以存储联系人的应用程序。在表视图中,我有姓氏显示,但希望也有名字和正确的日期。名字保存为名字,日期字段保存为日期。谁能告诉我怎么做这件事?
感谢 - (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];
}
这样做。在"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;
}
您想在主标签上同时显示姓和名,而在另一个标签上显示日期?还是三种都在同一个标签上?还是用三种不同的建议答案?
这可能是实现请求的最简单方法:
- (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;
}
如果你的联系人。date-variable是NSDate,看一下这个链接把日期放到NSString中,然后使用cell.detailTextLabel.text = stringFromDate;