When Ever I try to load data from an array into my table using custom cells it only shows the last cell like its overwriting all the cells until I Get to the end of the list. The table was created programmatically and when the cells don't load the blank table scrolls. If however I load the cells then only the last cell shows up and it won't scroll.
I figure I made a typo or something but I can't really see anything that would be of much help. The parser works and the array is fine its just that the cells are acting strange. I wanted to make it so that I had a fixed bar over the table as well but first I needed to get the custom cells to show up in the table and scroll properly
schedule.h
#interface Schedule : UIViewController <UITableViewDelegate, UITableViewDataSource> {
IBOutlet UITableView *newsTable;
UIActivityIndicatorView *activityIndicator;
CGSize cellSize;
NSXMLParser *fileParser;
NSMutableArray * events;
NSMutableArray * announcements;
NSMutableDictionary * item;
NSString *currentElement;
NSMutableString * currentTitle, * currentDate, * currentSummary, * currentId, * curr entTime, *currentContent;
UITableViewCell *appCell;
}
#property(nonatomic, retain)IBOutlet UITableView *newsTable;
#property(nonatomic, retain)IBOutlet UITableViewCell *appCell;
-(void)parseXMLFileAtURL:(NSString *)URL;
#end
schedule.m
- (void)loadView{
UITableView *tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]
style:UITableViewStylePlain];
tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
tableView.delegate = self;
tableView.dataSource = self;
tableView.tag = 4;
[tableView reloadData];
self.view = tableView;
[tableView release];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [events count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"MyCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil];
cell = appCell;
self.appCell = nil;
}
// Configure the cell.
UILabel *label;
label = (UILabel *)[cell viewWithTag:1];
int eventIndex = [indexPath indexAtPosition: [indexPath length] - 1];
label.text = [[events objectAtIndex: eventIndex] objectForKey: #"event"];
return cell;
}
- (void)parserDidEndDocument:(NSXMLParser *)parser {
NSLog(#"webpage parsed");
NSLog(#"events array has %d items", [events count]);
newsTable = (UITableView*)[self.view viewWithTag:4];
[newsTable reloadData];
}
I added more code the problem may be somewhere else.
I think your problem is that the cell is being released. When you call self.appCell = nil;, the object will be released and the variable set to nil. Try changing cell = appCell; to cell = [[appCell retain] autorelease]; so that the cell is retained long enough for the table view to retain it again.
Also, you could change int eventIndex = [indexPath indexAtPosition: [indexPath length] - 1]; to int eventIndex = indexPath.row;. The row and section properties were added to NSIndexPath specifically for use in UITableView, and it is guaranteed that the index path passed to cellForRowAtIndexPath: will contain exactly two indices.
try with following code;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = #"CellIdentifier";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; // CustomCell is the class for standardCell
if (cell == nil)
{
NSArray *objectList = [[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil];
for (id currentObject in objectList) {
if([currentObject isKindOfClass:[UITableViewCell class]]){
cell = (CustomCell*)currentObject;
break;
}
}
cell.label.text = [[events objectAtIndex: indexPath.row] objectForKey: #"event"];
}
return cell;
}
when you load the xib file with NSBundle, it will return an array of Bundle. Then you have to check it and then use it.
If you tried like;
NSString *memberName; // member variable
NSString *name = memberName;
memberName = nil;
// the member variable "memberName, giving the reference to name;
//So if you set memberName as nil, name also become a nil value.
// You have to use like;
name = [memberName retain];
memberName = nil;
Try to learn more about iPhone development documents.
Click here for Documentation.
Click here for sample code.
I think it will help you.
Related
Please help me with UITableViewCells.
I have followed many tutorials and sites but didn't get my problem solved.
I have tried to make a simpleCustomCells app but data are not loaded in the table view.
1.I have a table view controller:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [NSArray arrayWithArray:[[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil]];
for(id obj in nib)
{
if([obj isKindOfClass:[UITableViewCell class]])
{
cell = (CustomCell *)obj;
}
}
}
cell.label.text = #"hello";
return cell;
}
Also tried
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
"within if(cell==nill)"
2.I have created CustomCell : UITableViewCell with only a UILabel outlet. Changed CustomCell.xib identifier's name.
When I run this app I get this kind of error:
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSObject 0x71627f0> setValue:forUndefinedKey:]: this class
is not key value coding-compliant for the key label.
Please Help me with this issue, where am I going wrong.
Ok, I think I got where you are getting stuck.
Step 1 : Disconnect the outlet UILabel from the xib file.
Step 2 : Go to your xib file and you will see your objects list on the left hand side of the xib file.
Step 3 : Take your mouse pointer just below where objects is written. You will find Custom Cell written.
Step 4 : Press ctrl and drag it to the label and now Connect the Outlet to label.
Step 5 : Clean your program and Run it again.
All the best. If you still have doubts, I will elaborate. :)
I think you should add like this, I hope it works
static NSString *CellIdentifier = #"Cell";
customcellCell *cell = (customcellCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; <- set your customclass in this line.
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"customviewcell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.label.text = #"hello";
return cell;
Use following way :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ContactListCell *contactCell;
NSString *contactCellID = [NSString stringWithFormat:#"ContactCell%d%d",indexPath.row,indexPath.section];
contactCell = (ContactListCell *)[tableView dequeueReusableCellWithIdentifier:contactCellID];
if(!contactCell) {
contactCell = [[ContactListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:contactCellID];
contactCellID = nil;
}
strName = #"First name";
contactCell.lblFirstName.text = [strName capitalizedString];
return contactCell;
}
For Custom cell .h file
#interface ContactListCell : UITableViewCell {
UILabel *lblFirstName;
}
#property (strong, nonatomic) UILabel *lblFirstName;
#end
For Custom cell .m file
#implementation ContactListCell
#synthesize lblFirstName;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
lblFirstName = [[UILabel alloc]initWithFrame:CGRectMake(kxiPadFirstNameCellLandscape, kyiPadFirstNameCellLandscape, kiPadFirstNameCellWidthLandscape, kiPadFirstNameCellHeightLandscape)];
lblFirstName.backgroundColor= [UIColor clearColor];
lblFirstName.numberOfLines = 0;
lblFirstName.textColor = kCustomCellFontColor;
[self.contentView addSubview:lblFirstName];
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
#end
Why not give you're cell an identifier and instantiate it with dequewithreusableidentifier?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"Cell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[AanbodTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.Label.text =#"Hello"
return cell;
}
i would also refer to this very good tutorial http://www.appcoda.com/customize-table-view-cells-for-uitableview/
I am new to iphone.I have a small question that is,I have a class CustomCell in that i have declare all elements which i have required like the below
// CustomCell.h
here in this class i am setting the getter and setter methods to TitleLabel, PercentageLabel, ProgressView, downloadButton
// CustomCell.m
here in this class set the frame to above all,and set the image to the download button also here
screen shot of my output screen is
// In my table view i have a download button when we click that the following method will executes
-(void)downloadButtonClicked:(id)sender
{
NSLog(#"download button clicked");
int index = [sender tag];
NSLog(#"index of the cell is %d",index);
UIButton *button = (UIButton*)sender;
UITableViewCell *cell = (UITableViewCell *)[[button superview] superview];
UILabel *titleLabel = (UILabel *)[cell viewWithTag:100];
NSLog(#"label text =%#",titleLabel.text);
UIView *senderButton = (UIView*) sender;
NSIndexPath *indexPath = [tableView indexPathForCell: (UITableViewCell*)[[senderButton superview]superview]];
NSLog(#"indexpath is %d",indexPath.row);
CustomCell *customCell = [[CustomCell alloc]init];
progressView = [[UIProgressView alloc]init];
// [customCell.contentView insertSubview:progressView atIndex:indexPath.row];
[customCell.contentView addSubview:progressView];
selectedBookTitle = titleLabel.text;
NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSMutableArray *allDownloadLinks;
biblePlayerViewController = [[BiblePlayerViewController alloc]init];
allDownloadLinks = [biblePlayerViewController allDownloadLinks];
NSLog(#"all Download Links are %#",allDownloadLinks);
biblePlayerViewController.indexOfSelectedBookTitle = [[appDelegate getBookNames]indexOfObject:selectedBookTitle];
Download* download = [Download downloadWithTitle:selectedBookTitle url:[NSURL URLWithString:[NSString stringWithFormat:#"http://www.audiotreasure.com/%#.zip",[allDownloadLinks objectAtIndex:(biblePlayerViewController.indexOfSelectedBookTitle)]]]PathtoSave:documentsPath];
[[DownloadManager sharedDownloadManager] queueDownload: download];
}
// In cellForRowAtIndexPath tableview delegate method I have written code like this
- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
CustomCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
NSLog(#"indexpath.row is %d",indexPath.row);
NSLog(#"downloadmanager is %d",[[[DownloadManager sharedDownloadManager]getDownloads]count]);
/* if (indexPath.row<[[[DownloadManager sharedDownloadManager] getDownloads]count])
{
cell.TitleLabel.text = ((Download*)[[[DownloadManager sharedDownloadManager] getDownloads] objectAtIndex:indexPath.row]).title_;
cell.ProgressView.progress = (float)(((Download*)[[[DownloadManager sharedDownloadManager] getDownloads] objectAtIndex:indexPath.row]).PercentageDownloaded_ )/100.0f;
cell.PercentageLabel.text = [NSString stringWithFormat:#"%d %%",((Download*)[[[DownloadManager sharedDownloadManager] getDownloads] objectAtIndex:indexPath.row]).PercentageDownloaded_];
}
else
{
[tableView reloadData];
}*/
NSString *titleLabel = [[appDelegate getBookNames]objectAtIndex:indexPath.row];
cell.TitleLabel.text = titleLabel;
cell.downloadButton.tag = indexPath.row;
NSLog(#"tag is %d",cell.downloadButton.tag);
[cell.downloadButton addTarget:self action:#selector(downloadButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
My question is I have 66 cells in tableview in screen shot we have a bookname called Revelation.It has a 66th cell in my tableview (index is 65).Here when i click the download button in that cell,I have to show progress view in that particular cell only (65th cell).I have reload the table view for every 1 sec also because after placing the progress view we have to show the progress in that progress view in that cell.So, how can we place a progress view in that particular cell index.
Why don't you add the progress view in the cell from the beginning?
have it initially hidden and when you touch the download button you tell the cell to show it.
i see that you store the indexPath.row of the cell in the download button tag. You could use that to find out what cell was selected something like below.
NSIndexPath *cellIndexPath = [NSIndexPath indexPathForRow:sender.tag inSection:0];
CustomCell *cell = (CustomCell *)[tableView cellForRowAtIndexPath:nowIndex];
[cell showProgressBar];
i wrote the code of top of my head..didn't tested it.
When i checked a cell, the image associated is changed, but when i had scrolled down and up, the cell got its default image this is my code:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
NSArray *listData =[self->tableContents objectForKey:
[self->sortedKeys objectAtIndex:[indexPath section]]];
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
if(cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [listData objectAtIndex:row];
UIImageView *unchecked = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"unchecked.png"]];
UIImageView *checked = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"checked.png"]];
BOOL bChecked = NO;//By default, it's unchecked, so NO
/// Assign cell according to `bChecked`
cell.accessoryView = (bChecked ? checked : unchecked);
return cell;
}
EDIT:
I have edited my code to look as above, but i still get the issue, the default image is getting back when i scroll up my table view.
EDIT 2:
#Openside: So according to your approach, my snippet should look like this:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
NSArray *listData =[self->tableContents objectForKey:
[self->sortedKeys objectAtIndex:[indexPath section]]];
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
if(cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier];
}
NSUInteger row = [indexPath row];
textClass *myText = (textClass*)[listData objectAtIndex:row];
cell.textLabel.text = myText.text;
UIImageView *unchecked = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"unchecked.png"]];
UIImageView *checked = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"checked.png"]];
cell.accessoryView = (myText.isChecked ? checked : unchecked);
return cell;
}
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
textClass *myText=[[textClass alloc]init];
myText.isChecked=YES;
}
I got this exception:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString text]: unrecognized selector sent to instance 0x673bc'
The line indicating the exception issue is this (in cellForRowAtIndexPath delegate method):
cell.textLabel.text = myText.text;
I have added the class textClass although i am not so convinced, i think i can use a property in the holder class of my UITableView.
As Openside said, while scrolling, the cell will come from dequeue. So, we need to assign image according to new state.
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
UITableViewCell *cell = ...; ///< reuse a created cell.
if(cell == nil) {
cell = ...; ///< create a new cell
}
BOOL bChecked = ...///< Decide the cell checked or not.
/// Assign cell according to `bChecked`
cell.accessoryView = (bChecked ? imgChecked : imgNonChecked);
return cell;
}
Edit:
As for me, I will decide check status by the method indexPathForSelectedRow of tableView. I assume your table view is not multiple selection. It selects only one row at one time.
So, I decide the checked status by following. Because the tableView holds the information about which indexPath is selected.
NSIndexPath *selIndexPath = [tableView indexPathForSelectedRow];
BOOL bChecked = [indexPath isEqual:selIndexPath];
The problem is that the cell will be refreshed (or reused hence the dequeueReusableCellWithIdentifier ) when you scroll it out of view and then back into view. You need to have a flag on your source object to say if it's checked or not and then add an if statement to determine if the checked.png or unchecked.png image is used.
However, looking at your code the source object is just text, it may be worth creating a subclassed NSObject with two properties
text and
checked
Then your cellForRowAtIndexPath code can determine which image to show.
I hope that helps.
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
if(cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier];
}
NSUInteger row = [indexPath row];
textClass *myText = (textClass*)[listData objectAtIndex:row];
cell.textLabel.text = myText.text;
UIImageView *unchecked = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"unchecked.png"]];
UIImageView *checked = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"checked.png"]];
/// Assign cell according to `bChecked`
cell.accessoryView = (myText.isChecked ? checked : unchecked);
return cell;
}
#interface textClass : NSObject {
#private
NSString *_text;
BOOL _isChecked;
}
#property (nonatomic, retain) NSString *text;
#property (nonatomic, assign) BOOL isChecked;
#end
#implementation textClass
#synthesize text = _text;
#synthesize isChecked = _isChecked;
#end
Ok so here I have created a class called textClass it has two properties text and isChecked. When you load your view populate an NSMutableArray (listData) with these objects using the text you were previously using. When your cell gets checked set the isChecked property to YES when the cell gets reused this property has retained its state outside of the delegate method and should render correctly.
Put the complete code in brackets :
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
NSArray *listData =[self->tableContents objectForKey:
[self->sortedKeys objectAtIndex:[indexPath section]]];
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
if(cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier];
NSUInteger row = [indexPath row];
textClass *myText = (textClass*)[listData objectAtIndex:row];
cell.textLabel.text = myText.text;
UIImageView *unchecked = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"unchecked.png"]];
UIImageView *checked = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"checked.png"]];
cell.accessoryView = (myText.isChecked ? checked : unchecked);
}
return cell;
}
This should definitely work.
Code:
In .h:
NSMutableArray *contentArray;
I'm declaring my array.
In .m
- (void)viewDidLoad
{
[super viewDidLoad];
contentArray = [[NSMutableArray alloc] initWithObjects:#"view", #"browse", #"create", nil];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
I'm setting it up in my view did load.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [contentArray count];
}
I'm setting the number of rows to the array count.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
[[cell textLabel] setText:[contentArray objectAtIndex:indexPath.row]];
return cell;
}
Nothing. But if I do "[[cell textLabel] setText:#"Hello World."];" instead, it works fine.
Are you at least getting 3 empty rows in table ? If yes then just change your code
NSString *tempString = [your_array objectAtIndex:indexPath.row];
cell.textLabel.text = tempString;
If you are not even getting empty strings then make property of your array in .h file. Synthesize it in .m (also release it in delloc function) and finally in viewDidLoad right following
NSMutableArray tempContentArray = [[NSMutableArray alloc] arrayWithObjects:#"view", #"browse", #"create", nil];
self.contentArray=tempArray;
and then write following code to get cell title
NSString *tempString = [self.contentArray objectAtIndex:indexPath.row];
cell.textLabel.text = tempString;
Try to initialize your Array in your init method or call reloadDataon your UITableView after you set your Array.
Your code in .m file is fine. Write property .h file synthesize and dealloc your NSMutable array .m file it may be help you.
simply use following line of code
//initlize nsarray with objects.
-(void)viewDidLoad
{
NSArray *practice_ArrayForDisplayingEx=[NSArray alloc]initWithObjects:#"bhupi",#"bhupi",#"bhupi",#"bhupi",nil];
}
//then use in tableview.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableItem";
// UIButton *practice_takeTest_button;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier]autorelease];
}
cell.textLabel.text =[practice_ArrayForDisplayingEx objectAtIndex:indexPath.row];
cell.textLabel.font=[UIFont fontWithName:#"Arial" size:15];
return cell;
}
make sure you included UITableViewDelegate & UITableViewDataSourse prtocol in .h file.
hope it will help you..
Make sure that your .h implements the tableview delegate and datasource... should look like this
#interface myClass : UITableViewController <UITableViewDelegate, UITableViewDataSource>
then make this code in your ViewDidLoad method
[self.tableView setDelegate:self];
[self.tableView setDataSource:self];
I am working on an application having UItableView. In the rows of the table i am able to put checkmarks.Now how to save the state of checkmarks so that even if user close the application
the state should be shaved and in the next launch of application checkmark should be shown.
i have followed the tutorials on NSUSerDefaults but Pulling my hairs where to put the codes of saving and retrieving.I have tried but every time errors are stuffing me and not able to fix.
My Code:
MY.h file
**#protocol LocationSelectionViewControllerDelegate <NSObject>
#required
- (void)rowSelected:(NSString *)selectedValue selectedIndex:(NSInteger)index;
#end**
#interface LocationSelection : UIViewController <UITableViewDelegate,UITableViewDataSource>{
UITableView *table;
***NSInteger selectedIndex;***
NSMutableArray *menuList;
***id <LocationSelectionViewControllerDelegate> delegate;***
}
#property (nonatomic,retain) NSMutableArray *menuList;
#property (nonatomic,retain) IBOutlet UITableView *table;
***#property (nonatomic, assign) id <LocationSelectionViewControllerDelegate> delegate;**
**#property NSInteger selectedIndex;***
#end
my .m file:
#implementation LocationSelection
***#synthesize menuList, table,selectedIndex,delegate;***
- (void)viewDidLoad
{
menuList = [[NSMutableArray alloc] initWithObjects:
[NSArray arrayWithObjects:#"LOCATION1", nil],
[NSArray arrayWithObjects:#"LOCATION2", nil],
[NSArray arrayWithObjects:#"LOCATION3", nil],
nil];
self.title = #"Location Selection";
[table reloadData];
[super viewDidLoad];
}
//MY CELLFORROWATINDEXPATH
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
static NSString *CellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [table dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero] autorelease];
}
cell.highlighted = NO;
***NSArray * rowArray = [menuList objectAtIndex:indexPath.row];***
UILabel * nameLabel = [[[UILabel alloc] initWithFrame:CGRectMake(15, 8, 200, 20)] autorelease];
nameLabel.text = [NSString stringWithFormat:#"%#", [rowArray objectAtIndex:0]];
[cell.contentView addSubview:nameLabel];
***cell.accessoryType = (rowArray == selectedIndex && selectedIndex > -1 && selectedIndex < [menuList count]) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
return cell;
***
}
//MY DIDSELECTROWATINDEXH
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
int newRow = [indexPath row];
if (newRow != selectedIndex)
{
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
if (selectedIndex > - 1 && selectedIndex < [menuList count])
{
NSUInteger newIndex[] = {0, selectedIndex};
NSIndexPath *lastIndexPath = [[NSIndexPath alloc] initWithIndexes:newIndex length:2];
UITableViewCell *oldCell = [tableView cellForRowAtIndexPath: lastIndexPath];
oldCell.accessoryType = UITableViewCellAccessoryNone;
}
selectedIndex = newRow;
NSString *selectedValue=[menuList objectAtIndex:selectedIndex];
[self.delegate rowSelected:selectedValue selectedIndex:selectedIndex];
}
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
}
Use this method to save your data to NSUserDefaults
And set the data in viewDidLoad of LocationSelection
Hi I think it will suit you if you have an object(s) to indicate the checkmark. You can save this to the user defaults through synchronize. In the cellForRowATIndexPath: you can check if the value is present in the user defaults and if yes make the cell accessory as checkmarked and if its not present make it none.
Put a state in your menuList which reflects selection of the cell so even multiselection can be done easily. You can use a dictionary instead of the arrays. Arrays are fine but not as readable, so you have to remember which field contains what and map it to an index.
When the view loads load the menuList array from your userDefaults, when the app closes save the defaults.
In didSelectRowAtIndexPath you save selection in menuList.
In cellForRowAtIndexPath you read menuList and the new array index/dictionary key and set the checkmark.