Jump to content
Sign in to follow this  
Paulo Cabral

Tableview IOS - Livro Desenvolvimento IOS

Recommended Posts

Paulo Cabral

Boa tarde a todos!

Por acaso acaso alguem tem o livro desenvolvimento IOS da fca? É que estou a tentar fazer a aplicação que esta no livro e nao consigo passar da parte das tableview!

O metodo cellForRowAtIndexPath: não esta a ser chamado, pois o numero de linha da secção é zero!!!

O objecto é criar uma tableview fazia com 2 secções e a medida que o utilizador vai adicionando artigos estes vao aparecendo na tableView!

Gratos pela ajuda!

Share this post


Link to post
Share on other sites
KTachyon

Não tenho o livro, mas se apresentares o problema que estás a ter e o código relevante, é provável que te saiba responder.

Se não há linhas para mostrar, é normal que o cellForRowAtIndexPath: nunca seja chamado. É assim que funciona. Se precisas que ele seja chamado por alguma razão estranha é porque estás a colocar lá código que não deveria lá estar.


“There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult.”

-- Tony Hoare

Share this post


Link to post
Share on other sites
Paulo Cabral

Pois, inicialmente não há linhas para chamar mas depois da inserção de um artigo já há e mesmo assim não amostra ...

Vou colocar aqui o código que acho relevante

//
//  MainViewController.m
//  OMeuDiario
//
//  Created by Paulo Cabral on 18/07/13.
//  Copyright © 2013 cabral.learning. All rights reserved.
//
#import "MainViewController.h"
#import "NewItemViewController.h"
#import "Repository.h"
#import "Item.h"
@interface MainViewController ()

-(void) refresh;
@end

#define LESS_THAN_ONE_WEEK_SECTION 0
#define MORE_THAN_ONE_WEEK_SECTION 1
@implementation MainViewController{
@private
NSArray  *_sections;
Repository *_repository;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
	// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.

_repository = [[Repository alloc] init];
_sections = [[NSMutableArray alloc] initWithObjects:[NSMutableArray array],[NSMutableArray array], nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)viewWillAppear:(BOOL)animated{
NSLog(@"teste will");
//[self refresh];
}

-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{

return [_sections count];
}

#pragma mark - tableView Data source methods
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{


NSLog(@"section %d number of rows %d",section,[[_sections objectAtIndex:section] count]);

return [[_sections objectAtIndex:section] count];

}
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"cell for row");

static NSString *CellIdentifier = @"myCell";


UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if(cell == nil)
{
	cell = [[uITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

	NSLog(@"cell is nil");
}


Item *item = [[_sections objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];

cell.textLabel.text = item.text;



return cell;
}

-(void ) refresh{


NSLog(@"i am a refresh");
NSArray *all = [_repository fetchAllItems];

NSMutableArray *_moreThanOneWeek = [_sections objectAtIndex:MORE_THAN_ONE_WEEK_SECTION];

NSMutableArray *_lessThanOneWeek = [_sections objectAtIndex:LESS_THAN_ONE_WEEK_SECTION];

[_moreThanOneWeek removeAllObjects];
[_lessThanOneWeek removeAllObjects];

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSDate *today = [[NSDate alloc] init];

NSUInteger unitFlags  = NSMonthCalendarUnit | NSWeekCalendarUnit;

for(Item *item in all){

	NSDateComponents *components = [gregorian components:unitFlags fromDate:item.createdAt toDate:today options:0];

	NSInteger weeks = [components week];

	NSLog(@"iten texto %@",item.text);

	if(weeks>1){
		[_moreThanOneWeek addObject:item];

	}else{
		[_lessThanOneWeek addObject:item];
	}
}

}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
if(section == LESS_THAN_ONE_WEEK_SECTION){
	return @"Última Semana";

}else{
	return @"Antigos";
}
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:@"showItem"]){


}else if([segue.identifier isEqualToString:@"newItem"]){

	NewItemViewController *target = (NewItemViewController *)segue.destinationViewController;



	target.repository = _repository;



}


}
@end

//
//  MainViewController.h
//  OMeuDiario
//
//  Created by Paulo Cabral on 18/07/13.
//  Copyright © 2013 cabral.learning. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface MainViewController : UIViewController
<UITableViewDataSource>{
NSArray *tableData;
}
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, retain) NSArray *tableData;

@end

Edited by Paulo Cabral

Share this post


Link to post
Share on other sites
KTachyon

A UITableView não se actualiza automaticamente quando inseres dados nos NSArrays. Podes forçar a tabela a recarregar toda a informação com:

[tableView reloadData];


“There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult.”

-- Tony Hoare

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.