Нет текста на tableView в Iphone - PullRequest
       18

Нет текста на tableView в Iphone

0 голосов
/ 15 сентября 2011

Я новичок в Iphone и мне нужна помощь для отображения текста в виде таблицы.

Вот мой код:

.h

@class MainMenuViewController;

@interface RecherchePartenaireViewController : UIViewController <UINavigationControllerDelegate> {

    MainMenuViewController *mainMenuViewController;

    UINavigationController *navigationController;

    GradientButton *rechercheButton;

    RecherchePartenaireResultatListeViewControleur *recherchePartenaireResultatListeViewControleur;

    IBOutlet UITableView *categorystable;
    NSMutableArray *listData;
}

@property (nonatomic, retain) IBOutlet MainMenuViewController *mainMenuViewController;

@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;

@property (nonatomic, retain) IBOutlet GradientButton *rechercheButton;

@property (nonatomic, retain) IBOutlet RecherchePartenaireResultatListeViewControleur *recherchePartenaireResultatListeViewControleur;

@property (nonatomic, retain) IBOutlet UITableView *categorystable;
@property(nonatomic, retain) NSArray *listData;

@end

а в .м у меня:

@synthesize mainMenuViewController, navigationController, rechercheButton, recherchePartenaireResultatListeViewControleur,listData;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{

    [mainMenuViewController release];
    [navigationController release];
    [rechercheButton release];

    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];

    listData = [[NSMutableArray alloc] initWithObjects:@"iPhone", @"iPod", @"iPad",nil];
    NSLog(@"hey %@",listData);

    [rechercheButton useRedDeleteStyle];

    // Do any additional setup after loading the view from its nib.

}

- (void)viewDidUnload
{
    self.rechercheButton = nil;
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];


    }

    // Set up the cell...
    cell.textLabel.text = @"label";

    /*[[cell textLabel] setText: [[listData objectAtIndex:indexPath.row] valueForKey:@"name"]];*/


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

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return listData;
}

@end

и я ничего не получаю от tableView. Где моя ошибка? Я видел много примеров в сети, но я не вижу, что я делаю неправильно. Кто-нибудь может мне помочь?

Ответы [ 6 ]

3 голосов
/ 15 сентября 2011

В своем файле Xib вы задали делегат и источник данных вашего tableView в качестве viewController?

2 голосов
/ 15 сентября 2011

Изменить, как это,

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [listData count];
}
2 голосов
/ 15 сентября 2011

Вы должны внести изменения ниже.

- (void)viewDidLoad
{
    [super viewDidLoad];

    listData = [[NSMutableArray alloc] initWithObjects:@"iPhone", @"iPod", @"iPad",nil];
    NSLog(@"hey %@",listData);

    [rechercheButton useRedDeleteStyle];

    // Do any additional setup after loading the view from its nib.

   [categorystable reloadData];
}

А также поменяй.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [listData count];
}
2 голосов
/ 15 сентября 2011

Если вы хотите использовать стандартный стиль ячеек в вашем UITableView, вам следует заменить строку, в которой вы создаете новую ячейку, на эту, например:

заменить

cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];

с помощью

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];

Вы также можете выбрать другой предопределенный стиль для ячейки: UITableViewCellStyleDefault, UITableViewCellStyleValue1, UITableViewCellStyleValue2, UITableViewCellStyleSubtitle

Также у вас есть ошибка в методе numberOfRowsInSection:.Это должно выглядеть так:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section        
{
    return [listData count];
}
1 голос
/ 15 сентября 2011
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return [listData count];//that many times cellFoeRowAt Index method Called.
  }
  in above case you are returning Array instead OF Count.

ура

1 голос
/ 15 сентября 2011

Если вы имеете в виду, что ваш tableView видим, но ячейки / содержимое не видны, возможно, вы можете попробовать установить значение для:

EDIT: имейте это в вашем .m, который установит высоту строки в 44px.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 44.0;
}

Также согласитесь с другими ответами о том, что вам нужно возвращать [listData count] вместо просто listData для этого метода.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...