首页 新闻 会员 周边 捐助

Xcode创建TableView的分区显示不正确

0
悬赏园豆:5 [已解决问题] 解决于 2016-07-16 20:46

运行时的截图:

每个分区的标题Red 和 Blue 都在分区的下面显示,正常来说应该是在分区的开头显示的

 

ViewController.m的代码如下:

  1 #import "ViewController.h"
  2 
  3 #define kSectionCount 2
  4 #define kRedSection 0
  5 #define kBlueSection 1
  6 
  7 @interface ViewController ()
  8 
  9 @property (strong, nonatomic) NSArray *redFlowers;
 10 @property (strong, nonatomic) NSArray *blueFlowers;
 11 
 12 @end
 13 
 14 @implementation ViewController
 15 
 16 - (void)viewDidLoad {
 17     
 18     [super viewDidLoad];
 19     // Do any additional setup after loading the view, typically from a nib.
 20     self.redFlowers = @[@"Gerbera", @"Peony", @"Rose", @"Poppy"];
 21     self.blueFlowers = @[@"Hyacinth", @"Hydrangea", @"Sea Holly", @"Phlox", @"Iris"];
 22     
 23 }
 24 
 25 - (void)didReceiveMemoryWarning {
 26     [super didReceiveMemoryWarning];
 27     // Dispose of any resources that can be recreated.
 28 }
 29 
 30 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
 31 {
 32     return kSectionCount;
 33 }
 34 
 35 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 36 {
 37     switch (section) {
 38         case kRedSection:
 39             return [self.redFlowers count];
 40         case kBlueSection:
 41             return [self.blueFlowers count];
 42         default:
 43             return 0;
 44     }
 45 }
 46 
 47 -(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
 48 {
 49     switch (section) {
 50         case kRedSection:
 51             return @"Red";
 52         case kBlueSection:
 53             return @"Blue";
 54         default:
 55             return @"Unknown";
 56     }
 57 }
 58 
 59 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 60 {
 61     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"flowerCell"];
 62     
 63     switch (indexPath.section) {
 64         case kRedSection:
 65             cell.textLabel.text = self.redFlowers[indexPath.row];
 66             break;
 67         case kBlueSection:
 68             cell.textLabel.text = self.blueFlowers[indexPath.row];
 69             break;
 70         default:
 71             cell.textLabel.text = @"Unknown";
 72             break;
 73     }
 74     
 75     UIImage *flowerImage;
 76     flowerImage = [UIImage imageNamed: cell.textLabel.text];
 77     cell.imageView.image = flowerImage;
 78     
 79     return cell;
 80 }
 81 
 82 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 83 {
 84     UIAlertView *showSelection;
 85     NSString *flowerMessage;
 86     
 87     switch (indexPath.section) {
 88         case kRedSection:
 89             flowerMessage = [NSString stringWithFormat:@"You chose the red flower - %@", self.redFlowers[indexPath.row]];
 90             break;
 91         case kBlueSection:
 92             flowerMessage = [NSString stringWithFormat:@"You chose the blue flower - %@", self.blueFlowers[indexPath.row]];
 93             break;
 94         default:
 95             flowerMessage = [NSString stringWithFormat:@"I have no idea what you chose!?"];
 96             break;
 97     }
 98     
 99     showSelection = [[UIAlertView alloc]initWithTitle:@"Flower Selected" message:flowerMessage delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
100     [showSelection show];
101 }
102 @end
SharpeyeKardel的主页 SharpeyeKardel | 初学一级 | 园豆:30
提问于:2016-07-16 20:36
< >
分享
最佳答案
0

找到问题所在了:

 1 -(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
 2 {
 3     switch (section) {
 4         case kRedSection:
 5             return @"Red";
 6         case kBlueSection:
 7             return @"Blue";
 8         default:
 9             return @"Unknown";
10     }
11 }

方法引用错了,

混淆了(NSString *)tableView:(UITableView *) titleForFooterInSection:(NSInteger)

和(NSString *)tableView:(UITableView *) titleForHeaderInSection:(NSInteger)这两个方法

将上述函数名修改成Header,其他不改变,问题就能解决了。

SharpeyeKardel | 初学一级 |园豆:30 | 2016-07-16 20:45
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册