1

名前などで4つのセクションに分割したTableViewの色とフォントを変更するのに問題があります。機能しないようです。何が間違っているのかわかりませんか?

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

3 に答える 3

4

文字列の代わりにビューを返す必要があります...

これはあなたが返しているものです

return sectionName;

そして、これはあなたが返すべきものです。

return sectionHeader;
于 2012-06-27T08:23:20.890 に答える
2
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

UITableViewのヘッダーにビューを表示するための正しいメソッドです。ここで、UILabelオブジェクトを返すことができます。

UILabelの代わりにNSStringオブジェクトを返そうとしています。また、メソッドの戻りタイプが間違っています。タイプはUIViewである必要があります。

http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UITableViewDelegate_Protocol/Reference/Reference.html

于 2012-06-27T08:23:42.027 に答える
1
-(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を返します。NSStringはUIViewを返すことになっています。

于 2012-06-27T08:22:41.310 に答える