Loading data in NSUserDefaults - cocoa-touch

i have done the coding to save the highscore in NSuserdefaults, but i am not sure how can i load the data from the nsuserdefaults and display it in a table. PLease help.
NSString *name;
name = nametextbox.text;
NSDictionary *player = [NSDictionary dictionaryWithObjectsAndKeys: [NSString stringWithFormat:#"%#", name], #"name",[NSString stringWithFormat:#"%d", myScore], #"score",nil];
[highScore addObject:player];
NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:#"score" ascending:NO];
[highScore sortUsingDescriptors:[NSArray arrayWithObject:sort]];
[sort release];
[[NSUserDefaults standardUserDefaults] setObject:highScore forKey:#"highScore"];

You should be able to load it just the way you'd expect (like accessing a value in an NSDictionary):
NSArray *highScore = [[NSUserDefaults standardUserDefaults] objectForKey:#"highScore"];
Update
To display the data from this array in a table view, you'll need to create a view controller and use the array as your data source. The easiest way to do this is by subclassing UITableViewController. This should get you started on the implementation of that controller:
// HighScoreViewController.h
#interface HighScoreViewController : UITableViewController {
NSArray *highScores;
}
#property (nonatomic, retain) NSArray *highScores;
#end
.
// HighScoreViewController.m
#import HighScoreViewController.h
static const NSInteger kNameLabelTag = 1337;
static const NSInteger kScoreLabelTag = 5555;
#implementation HighScoreViewController
#synthesize highScores;
- (void)viewDidLoad {
[self setHighScores:[[NSUserDefaults standardUserDefaults]
objectForKey:#"highScore"]];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
return [self.highScores count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"PlayerCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cel == nil) {
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:cellIdentifier] autorelease];
// Create UILabels for name and score and add them to your cell
UILabel *nameLabel = [[UILabel new] autorelease];
[nameLabel setTag:kNameLabelTag];
[cell.contentView addSubview:nameLabel];
UILabel *scoreLabel = [[UILabel new] autorelease];
[scoreLabel setTag:kScoreLabelTag];
[cell.contentView addSubview:scoreLabel];
// Set other attributes common to all of your cells here
// You will also need to set the frames of these labels (nameLabel.frame = CGRectMake(...))
}
NSDictionary *player = [self.highScores objectAtIndex:indexPath.row];
NSString *name = [player objectForKey:#"name"];
NSString *score = [player objectForKey:#"score"];
[(UILabel *)[cell.contentView viewWithTag:kNameLabelTag] setText:name];
[(UILabel *)[cell.contentView viewWithTag:kScoreLabelTag] setText:score];
return cell;
}
#end
A key thing to remember with UITableView is that cells get reused, so you need to be careful about where you initialize/configure your cell's subviews.

Related

How to get data from property list from Documents?

I'm new to the Objective-C
I want to read a file.plist from path Documents:
/4BC1E3AF-15AD-4F33-A55D-E52C60F13DE8/data/Containers/Data/Application/95BD9BE3-5A5A-4EAC-A9FE-133EF64D1D1A/Documents"
Can you help me replace this line of code?:
NSDictionary *dictRoot = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle]pathForResource:#"TestPlist" ofType:#"plist"]];
Full code:
here
#import "ViewController.h"
#interface ViewController () {
NSMutableArray *subjectList;
NSMutableArray *contentList;
}
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
subjectList = [[NSMutableArray alloc]init];
contentList = [[NSMutableArray alloc]init];
NSDictionary *dictRoot = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle]pathForResource:#"TestPlist" ofType:#"plist"]];
NSArray *arrayList = [NSArray arrayWithArray:[dictRoot objectForKey:#"Data"]];
[arrayList enumerateObjectsUsingBlock:^(id obj, NSUInteger index, BOOL *stop) {
[subjectList addObject:[obj valueForKey:#"Title"]];
[contentList addObject:[obj valueForKey:#"Content"]];
}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [subjectList count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *simpleTableIdentifier = #"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if(cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [subjectList objectAtIndex:indexPath.row];
cell.detailTextLabel.text = [contentList objectAtIndex:indexPath.row];
return cell;
}
#end
You get the URL of the Documents folder in the container with
NSURL *documentsFolderURL = [[NSFileManager defaultManager]
URLForDirectory: NSDocumentDirectory
inDomain: NSUserDomainMask
appropriateForURL: nil
create: false
error: nil];
NSDictionary *dictRoot = [NSDictionary dictionaryWithContentsOfURL:[documentsFolderURL URLByAppendingPathComponent:#"TestPlist.plist"] error: nil];
There is also dictionaryWithContentsOfURL although I recommend to prefer NSPropertyListSerializtion

populate uitableview as subview of uicollectionview

I have a problem in populating a uitableview created programmatically as a subview of uicollectionview.
here's my code:
.h file
#import <UIKit/UIKit.h>
#interface TheViewController : UICollectionViewController<UITableViewDataSource>
#property (atomic,strong) IBOutlet UIBarButtonItem *plus;
-(IBAction) addTeamPressed:(id)sender;
#end
.m file
- (void)viewDidLoad
{
[super viewDidLoad];
NSError *jsonParsingError = nil;
teamsView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.window.bounds.size.width, self.view.window.bounds.size.height)];
NSData *t = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://..."] options:NSDataReadingMappedIfSafe error:&jsonParsingError];
NSArray *tmp = [NSJSONSerialization JSONObjectWithData:t options:0 error:&jsonParsingError];
UITableViewCell * teamCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
teams = [[NSMutableArray alloc]init];
[teamsView addSubview:teamCell];
teamsView.delegate = self;
teamsView.dataSource = self;
for(int i =0;i<tmp.count;i++){
[teams addObject:[tmp objectAtIndex:i]];
}
plus.title = #"Done";
[self.view addSubview:teamsView];
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
// If You have only one(1) section, return 1, otherwise you must handle sections
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [teams count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
TeamsNameCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[SquadreNameCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.name.text = [NSString stringWithFormat:#"%#",[[teams objectAtIndex:indexPath.row] objectForKey:#"name"]];
NSLog(#"%d teams",[teams count]);
return cell;
}
my tableview is displaying correctly, of course thanks to other functions not mentioned here, but no rows in the subview.
the compiler prints out the number of teams, so it calls the delegate methods, but it doesn't populate the subview...
what 's wrong with this?
thanks in advance
Dario
First you should remove this from your viewDidLoad:
UITableViewCell * teamCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
[teamsView addSubview:teamCell];
Then make sure that your teams array is retained, just put this line to the header file:
#property (nonatomic, strong) NSMutableArray* teams;
And then use self.teams instead of just teams
At the end of viewDidLoad method you should call [teamsView reloadData];
Also it would be good to change your table data source method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
TeamsNameCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
// please make sure that you allocate required object here
cell = [[TeamsNameCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// please make sure that you do have `name` property in your `TeamsNameCell` class
cell.name.text = [NSString stringWithFormat:#"%#",[[teams objectAtIndex:indexPath.row] objectForKey:#"name"]];
NSLog(#"%d teams",[teams count]);
return cell;
}
Please make sure that your TeamsNameCell class has name property in the header:
#property(nonatomic, strong) UILabel* name;
And also do not forget to allocate this label in the constructor of TeamsNameCell:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.name = [[UILabel alloc] initWithFrame:CGRectMake(0,0,100,50)];
[self addSubview:self.name];
}
return self;
}
Or Maybe it is better to use default cell's label:
instead of cell.name.text = #"text"; use cell.textLabel.text = #"text";
solution is,
static NSString *CellIdentifier = #"Cell";
TeamsNameCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[SquadreNameCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.labelText.text = [NSString stringWithFormat:#"%#",[[teams objectAtIndex:indexPath.row] objectForKey:#"name"]];
NSLog(#"%d teams",[teams count]);
return cell;

how to use custom cell in objective-c

I want to create 4 cell "button or picture" in one row in uiTableView like this picture:
but I don't know how can I do that :
would you please help me!
Thanks in advance!
here is my code :
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableArray *keys = [[NSMutableArray alloc] init];
NSMutableDictionary *contents = [[NSMutableDictionary alloc] init];
NSString *monKey = #"Monday";
NSString *tueKey = #"Tuesday";
NSString *wedKey = #"Wednday";
NSString *thuKey = #"Thusday";
NSString *friKey = #"Friday";
NSString *satKey = #"Satuarday";
NSString *sunKey = #"Sunnday";
[contents setObject:[NSArray arrayWithObjects:#"Work Time", #"Absence", nil] forKey:monKey];
[contents setObject:[NSArray arrayWithObjects:#"Compensation", #"Work Time", #"Absence", nil] forKey:wedKey];
[contents setObject:[NSArray arrayWithObjects:#"Compensation", #"Work Time", nil] forKey:tueKey];
[contents setObject:[NSArray arrayWithObjects:#"Compensation", #"Work Time", nil] forKey:thuKey];
[contents setObject:[NSArray arrayWithObjects:#"Compensation", #"Work Time", nil] forKey:friKey];
[contents setObject:[NSArray arrayWithObjects:#"Compensation", #"Work Time", nil] forKey:satKey];
[contents setObject:[NSArray arrayWithObjects:#"Compensation", #"Work Time", nil] forKey:sunKey];
[keys addObject:tueKey];
[keys addObject:monKey];
[keys addObject:wedKey];
[keys addObject:thuKey];
[keys addObject:friKey];
[keys addObject:satKey];
[keys addObject:sunKey];
[self setSectionKeys:keys];
[self setSectionContents:contents];
self.navigationItem.leftBarButtonItem = self.editButtonItem;
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self action:#selector(addNewItem)];
self.navigationItem.rightBarButtonItem = rightButton;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *key = [[self sectionKeys] objectAtIndex:[indexPath section]];
NSArray *contents = [[self sectionContents] objectForKey:key];
NSString *contentForThisRow = [contents objectAtIndex:[indexPath row]];
static NSString *CellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
[[cell textLabel] setText:contentForThisRow];
int column = 4;
for (int i=0; i<column; i++) {
UIImageView *aBackgroundImageView = [[UIImageView alloc]initWithFrame:CGRectMake(32+184*i,10, 167,215)];
aBackgroundImageView.tag = (column*indexPath.row)+i;
[cell.contentView addSubview:aBackgroundImageView];
// [aBackgroundImageView release];
}
return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if(section == 0)
return #"Monday";
else if(section == 1){
return #"Tuesday";
}else if(section == 2){
return #"Wednesday";
} else if(section == 3){
return #"Thuesday";
} else if(section == 4){
return #"Friday";
} else if(section == 5){
return #"Saturday";
}else
return #"Sunday";
}
Edit 1
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithFrame:CGRectZero];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
int column = 4;
for (int i=0; i<column; i++) {
UIImageView *aBackgroundImageView = [[UIImageView alloc]initWithFrame:CGRectMake(32+184*i,10, 167,215)];
aBackgroundImageView.tag = (column*indexPath.row)+i;
[cell.contentView addSubview:aBackgroundImageView];
}
return cell;
}
You can design a custom cell with 4 button in Xcode and map them to tableview
If you don't need user interaction, you can add them to a UIView (each is a subview with an appropriate frame), and make the container view the cell's backgroundview.
You can add the views directly to the cell's contentView and get user interaction, but you have to be careful as that area gets modified if there is a accessoryView etc.
It will be easier to design the custom cell and lay out your 4 buttons (since you need user interaction) in interface builder. Here's a very good tutorial to get you started. Give a tag number to each of the button so you will know which button was tapped later.
want to create 4 cell "button or picture" in one row in uiTableView like this picture:
but I don't know how can I do that :
First of all, you will need to create an objective-c class called MyCell. MyCell will be a subclass of UITableViewCell (this is very important)
In your MyCell.h,
#interface MyCell : UITableViewCell
#property (nonatomic, weak) IBOutlet UIButton *firstButton;
#property (nonatomic, weak) IBOutlet UIButton *secondButton;
#property (nonatomic, weak) IBOutlet UIButton *thirdButton;
#property (nonatomic, weak) IBOutlet UIButton *fourthButton;
#end
Then in your MyCell.m, just synthesize all those buttons.
In your tableview, where you want to use your customized cell, you need to modify the cellForRowAtIndexPath method. Replace your UITableViewCell with your custom MyCell. And don't forget to import "MyCell.h" into that tableview that you are using.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
MyCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(!cell)
{
cell = [[MyCell alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
[cell.firstButton setTintColor:[UIColor redColor];
[cell.secondButton setTintColor:[UIColor redColor];
// and so on.. til you set all your four buttons.
return cell; }
And from there, your tableview should be showing your custom cell.

Table check box just for one section objective-c

I have a uiTableView with 3 sections and different rows, I want to add check box JUST to my third sections,
I create custom cell and I linke img and label
like this picture:
![enter image description here][1]
and my code for custom cell is :
.h
: UITableViewCell
#property (strong, nonatomic) IBOutlet UIImageView *checkBox;
#property (strong, nonatomic) IBOutlet UILabel *absenceCode;
#end
.m
#synthesize checkBox;
#synthesize absenceCode;
- (id)initWithCoder:(NSCoder *)coder
{
self = [super initWithCoder:coder];
if (self) {
// Initialization code
checkBox.image = [UIImage imageNamed:#"emptycheck-box.png"];
}
return self;
}
#end
and code for UITableViewController
viewDidLoad
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableArray *keys = [[NSMutableArray alloc] init];
NSMutableDictionary *contents = [[NSMutableDictionary alloc] init];
NSString *staKey = #"Start";
NSString *endKey = #"End";
NSString *absKey= #"Absence";
[contents setObject:[NSArray arrayWithObjects:#"Time: 08:00 Date: Fri,3 Aug, 2012", nil] forKey:staKey];
[contents setObject:[NSArray arrayWithObjects:#"Time: 17:57 Date: Fri,3 Aug, 2012", nil] forKey:endKey];
[contents setObject:[NSArray arrayWithObjects:#"Red",#"Black",#"Blue", nil] forKey:absKey];
[keys addObject:staKey];
[keys addObject:endKey];
[keys addObject:absKey];
[self setSectionKeys:keys];
[self setSectionContents:contents];
}
cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *key = [[self sectionKeys] objectAtIndex:[indexPath section]];
NSArray *contents = [[self sectionContents] objectForKey:key];
NSString *contentForThisRow = [contents objectAtIndex:[indexPath row]];
CheckBoxTableViewCell *cell = (CheckBoxTableViewCell*)[tableView
dequeueReusableCellWithIdentifier:#"CheckBoxTableViewCell"];
cell.imageView.image=[UIImage imageNamed:#"emptycheck-box.png"];
cell.checkBox.image = image;
cell.absenceCode.text =#"Redddd";
cell.text =contentForThisRow;
return cell;
}
would you please help me
Thanks in advance!
Something like the following should do what you want.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *key = [[self sectionKeys] objectAtIndex:[indexPath section]];
NSArray *contents = [[self sectionContents] objectForKey:key];
NSString *contentForThisRow = [contents objectAtIndex:[indexPath row]];
if (indexPath.section != 2) {
//Set up default cell here.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier
cell.textLabel.text =contentForThisRow;
return cell;
}
else {
CheckBoxTableViewCell *cell = (CheckBoxTableViewCell*)[tableView dequeueReusableCellWithIdentifier:#"CheckBoxTableViewCell"];
cell.imageView.image=[UIImage imageNamed:#"emptycheck-box.png"];
cell.checkBox.image = image;
cell.absenceCode.text =#"Redddd";
return cell;
}
}

Why am I getting a message sent to deallocated instance error in this case?

I am trying to sort a NSMutableArray based on an object it contains. I am getting an error on this line in the code segment below:
InboxItem * ptrInboxItem = [sortedInboxFaxItems objectAtIndex:[indexPath row]];
#import <UIKit/UIKit.h>
#class InboxItem;
#interface InboxTableViewController : UITableViewController<NSXMLParserDelegate> {
NSMutableArray *inboxFaxItems;
NSArray * sortedInboxFaxItems;
InboxItem *_inboxItem;
NSMutableData *xmlData;
NSURLConnection *connectionInprogress;
NSMutableString *inboxFaxesString;
UIActivityIndicatorView *activityIndicator;
}
#property(nonatomic,retain) InboxItem * inboxItem;
-(void) loadInbox;
#end
- (void) connectionDidFinishLoading:(NSURLConnection *)connection{
NSXMLParser *parser = [[NSXMLParser alloc]initWithData:xmlData];
[parser setDelegate:self];
[parser parse];
[parser release];
//lets sort by messageID
NSSortDescriptor *sortDescriptor;
sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:#"messageID" ascending:YES] autorelease];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
sortedInboxFaxItems = [inboxFaxItems sortedArrayUsingDescriptors:sortDescriptors];
[[self tableView] reloadData];
activityIndicator.stopAnimating;
[connectionInprogress release];
connectionInprogress = nil;
[xmlData release];
xmlData = nil;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"InboxFaxItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
//I AM GETTING ERROR HERE
InboxItem * ptrInboxItem = [sortedInboxFaxItems objectAtIndex:[indexPath row]];
[[cell textLabel]setText: ptrInboxItem.datetime];
cell.imageView.image = [UIImage imageNamed:#"document.png"];
return cell;
}
#pragma mark -
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic may go here. Create and push another view controller.
MyManager *sharedManager = [MyManager sharedManager];
InboxItem * ptrInboxItem = [sortedInboxFaxItems objectAtIndex:[indexPath row]];
sharedManager.pages = ptrInboxItem.pages;
sharedManager.from =ptrInboxItem.from;
FaxViewController *faxViewController = [[FaxViewController alloc] initWithNibName:#"FaxViewController" bundle:nil];
faxViewController.messageid=ptrInboxItem.messageID;
faxViewController.navigationItem.title=#"View Fax";
[self.navigationController pushViewController:faxViewController animated:YES];
[faxViewController release];
}
It's because of this line:
sortedInboxFaxItems = [inboxFaxItems sortedArrayUsingDescriptors:sortDescriptors];
You're assigning an object you don't own to an instance variable. Later, when you try to access it, that object has been deallocated, so your instance variable now points to garbage.
You should change that line to this:
[sortedInboxFaxItems release]; // Release any previous value
sortedInboxFaxItems = [[inboxFaxItems sortedArrayUsingDescriptors:sortDescriptors] retain];
All better.