我在更改TableView上的颜色和字体时遇到了一些问题,我已经将它分成了4个部分,带有名称等,我似乎无法让它工作我不确定我做错了什么?
- (NSString *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
NSString *sectionName = nil;
switch(section)
{
case 0:
sectionName = [NSString stringWithString:@"Date"];
break;
case 1:
sectionName = [NSString stringWithString:@"Gig"];
break;
case 2:
sectionName = [NSString stringWithString:@"City"];
break;
case 3:
sectionName = [NSString stringWithString:@"Country"];
break;
}
UILabel *sectionHeader = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 40)] autorelease];
sectionHeader.backgroundColor = [UIColor clearColor];
sectionHeader.font = [UIFont boldSystemFontOfSize:18];
sectionHeader.textColor = [UIColor whiteColor];
sectionHeader.text = sectionName;
return sectionName;
}发布于 2012-06-27 16:23:21
你应该返回视图而不是字符串...
这就是您要返回的内容
return sectionName;这是你应该返回的..
return sectionHeader;发布于 2012-06-27 16:23:42
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section是在UITableView的头中显示视图的正确方法。在这里,您可以返回UILabel对象。
您正在尝试返回NSString对象,而不是UILabel。另外,方法的返回类型也是错误的。它的类型应该是UIView。
http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UITableViewDelegate_Protocol/Reference/Reference.html
发布于 2012-06-27 16:22:41
-(UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section{
NSString *sectionName = nil;
switch(section)
{
case 0:
sectionName = [NSString stringWithString:@"Date"];
break;
case 1:
sectionName = [NSString stringWithString:@"Gig"];
break;
case 2:
sectionName = [NSString stringWithString:@"City"];
break;
case 3:
sectionName = [NSString stringWithString:@"Country"];
break;
}
UILabel *sectionHeader = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 40)] autorelease];
sectionHeader.backgroundColor = [UIColor clearColor];
sectionHeader.font = [UIFont boldSystemFontOfSize:18];
sectionHeader.textColor = [UIColor whiteColor];
sectionHeader.text = sectionName;
return sectionHeader;}
该代码向您展示了如何实现TableViewCOntroller的viewForHeaderInSection委托方法,您需要实现该方法才能完成所需的任务。您的代码返回NSString,它应该返回UIView。
https://stackoverflow.com/questions/11222060
复制相似问题