首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >sectionName TableView -我做错了什么?

sectionName TableView -我做错了什么?
EN

Stack Overflow用户
提问于 2012-06-27 16:19:44
回答 3查看 329关注 0票数 1

我在更改TableView上的颜色和字体时遇到了一些问题,我已经将它分成了4个部分,带有名称等,我似乎无法让它工作我不确定我做错了什么?

代码语言:javascript
复制
- (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;
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-06-27 16:23:21

你应该返回视图而不是字符串...

这就是您要返回的内容

代码语言:javascript
复制
return sectionName;

这是你应该返回的..

代码语言:javascript
复制
return sectionHeader;
票数 4
EN

Stack Overflow用户

发布于 2012-06-27 16:23:42

代码语言:javascript
复制
- (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

票数 2
EN

Stack Overflow用户

发布于 2012-06-27 16:22:41

代码语言:javascript
复制
-(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。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11222060

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档