【问题标题】:iPhone UITableView Sections not working [duplicate]iPhone UITableView 部分不起作用[重复]
【发布时间】:2011-06-22 21:19:41
【问题描述】:

可能重复:
iPhone UITableView Sections

基本上尝试两个的人将表格分成两个不同的部分,但无论我尝试什么似乎都不起作用,有我的代码请你告诉我哪里出错了,示例将不胜感激!还没有人设法解决这个问题,我真的被困了一段时间。谢谢山姆·霍顿。

#import <Foundation/Foundation.h>


@interface FirstLevelViewController : UITableViewController {
    NSArray *controllers;
    NSMutableArray *male;
    NSMutableArray *female;
}

@property (nonatomic, retain) NSArray *controllers;
@property (nonatomic, retain) NSMutableArray *male;
@property (nonatomic, retain) NSMutableArray *female;

@end



#import "FirstLevelViewController.h"
#import "SecondLevelViewController.h"
#import "DisclosureDetailController.h"
#import "SecondVoiceController.h"
#import "ThirdVoiceController.h"

@implementation FirstLevelViewController
@synthesize controllers,male,female;

-(void)viewDidLoad {
    self.title = @"Voices";
    DisclosureDetailController *th = [DisclosureDetailController alloc];
    th.title = @"First Voice";
    [male addObject:th];
    [th release];

    SecondVoiceController *array2 = [SecondVoiceController alloc];
    array2.title = @"Second Voice";
    [male addObject:array2];
    [array2 release];

    ThirdVoiceController *array3 = [ThirdVoiceController alloc];
    array3.title = @"Third Voice";
    [female addObject:array3];
    [array3 release];

    self.controllers = male;
    [female release];
    [male release];
    [super viewDidLoad];
}

-(void)viewDidUnload {
    self.male = nil;
    self.female = nil;
    self.controllers = nil;
    [super viewDidUnload];
}

-(void)dealloc {
    [male release];
    [female release];
    [controllers release];
    [super dealloc];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
         switch(section){
                case 0:
                return [male count];
                break;
                case 1:
                return [female count];
                break;
             }
    }

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
     static NSString *FirstLevelCell= @"FirstLevelCell";

     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:FirstLevelCell];
     if (cell == nil) {
         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:FirstLevelCell] autorelease];
     }
     SecondLevelViewController *controller;
     switch([indexPath section]){
         case 0:
              controller = [male objectAtIndex: [indexPath row] ];
              break;
         case 1:
              controller = [female objectAtIndex: [indexPath row] ];
              break;
     }
     cell.textLabel.text = controller.title;
     cell.imageView.image = controller.rowImage;
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
     return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSUInteger row = [indexPath row];
    SecondLevelViewController *nextViewController = [self.controllers objectAtIndex:row];
    [self.navigationController pushViewController:nextViewController animated:YES];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return NO;
}

@end

基本上尝试两个的人将表格分成两个不同的部分,但无论我尝试什么似乎都不起作用,有我的代码请你告诉我哪里出错了,示例将不胜感激!还没有人设法解决这个问题,我真的被困了一段时间。谢谢山姆霍顿

【问题讨论】:

  • 不完全确定您要做什么,但这看起来肯定是错误的: DisclosureDetailController *th = [DisclosureDetailController alloc];你应该有 [[DisclosureDetailController alloc] init];
  • 您能否更新您的帖子以使其使用代码块?
  • 好吧,我正在尝试创建一个表格视图,其中包含三个单元格,所有单元格都推送一个新视图在桌子上的另一部分女性中,我不知道如何将我的三个单元格分成不同的部分,谢谢 Sam Houghton
  • 粘贴您的代码(没有格式化!)并说“它不工作”不是寻求帮助的好方法。

标签: iphone objective-c xcode uitableview sections


【解决方案1】:

你的代码有很多问题。

  1. 下次请格式化您的代码。 :)
  2. 在几个地方,你正在这样做:

    DisclosureDetailController *th = [DisclosureDetailController alloc];
    ...
    SecondVoiceController *array2 = [SecondVoiceController alloc];
    ...
    ThirdVoiceController *array3 = [ThirdVoiceController alloc];
    

    您必须从不在未初始化对象的情况下使用它。您必须在使用 init 方法(或适当的初始化程序)之前调用它。

  3. 您的属性被初始化为nil。您永远不会创建它们应该指向的 NSMutableArray 对象。因此,[male count][female count] 将始终返回 0。换句话说,简单地声明一个属性与初始化它是不同的。

【讨论】:

    【解决方案2】:

    显然,由于格式的原因,代码有点难以阅读,所以如果我看错了什么,我很抱歉。但是在向它们添加任何对象之前,您需要调用male = [[NSMutableArray alloc] init];female = [[NSMutableArray alloc] init];。如果未分配数组,它们将不会响应addObject。调用addObject 时不会出现任何错误,如果你没有先分配并初始化它,它就不会这样做。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-19
      • 1970-01-01
      • 2016-07-15
      相关资源
      最近更新 更多