CoreDataBooks Add new cell to tableview - iphone

I recently downloaded Apple's sample app CoreDataBooks and for the sake of learning decided to add a cell to the last record in the RootViewController.m table view, which would display the number of records that were fetched.
The code I added is shown below. I didn't get to the point of adding the fetch count because I am getting bad results from my code. If you download CoreDataBooks and replace the table view datasource methods with the code here you will see the following:
the new cell is added to the bottom of the table view.
when you scroll back to the top of the table another cell receives the formatting that is only supposed to be given to the last cell of the table. In my case, under the author section of 'Bill Bryson' the book title 'Notes From a Big Country' turns blue and is centered when you scroll back to the top of the table.
Any help in resolving this would be appreciated.
Here is the link for the sample app CoreDataBooks: link text
Here is the modified code for RootViewController.m:
/*
The data source methods are handled primarily by the fetch results controller
*/
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [[fetchedResultsController sections] count] + 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section < [[fetchedResultsController sections] count])
{
id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
return 1;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath*)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSLog(#"indexPath.section: %i",indexPath.section);
if (indexPath.section < [[fetchedResultsController sections] count]){
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell.
[self configureCell:cell atIndexPath:indexPath];
}
else{
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = (#"This is a NEW Cell...");
cell.textLabel.textAlignment = UITextAlignmentCenter;
cell.textLabel.textColor = [UIColor blueColor];
}
return cell;
}
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
// Configure the cell to show the book's title
Book *book = [fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = book.title;
}
- (NSString *)tableView:(UITableView *)tableView
titleForHeaderInSection:(NSInteger)section {
// Display the authors' names as section headings.
if (section < [[fetchedResultsController sections] count])
return [[[fetchedResultsController sections] objectAtIndex:section] name];
return #"";
}
That's all. Thank-you for your time.

I see two solutions:
Use a different cell identifier for your cell.
To do this, define a new cell identifier after the other and move the cell reuse code inside the two different cell cases:
static NSString *CellIdentifier = #"Cell";
static NSString* myCellIdentifier = #"MyCell"; // new cell identifier
// move inside two cell cases
// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSLog(#"indexPath.section: %i",indexPath.section);
if (indexPath.section < [[fetchedResultsController sections] count]){
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; // only reuse this type of cell here
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell.
[self configureCell:cell atIndexPath:indexPath];
}
else{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:myCellIdentifier]; // only reuse this type of cell here
if (cell == nil) {
2 . Override all of the applicable formatting when configuring the cells:
cell.textLabel.text = (#"This is a NEW Cell..."); // You are already overriding the only cell attribute that the book cell configures
cell.textLabel.textAlignment = UITextAlignmentCenter;
cell.textLabel.textColor = [UIColor blueColor];
}
return cell;
}
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
// Configure the cell to show the book's title
Book *book = [fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = book.title;
cell.textLabel.textAlignment = UITextAlignmentLeft; // I assume Left is the default alignment
cell.textLabel.textColor = [UIColor blackColor]; // I assume black is the default color
}

Related

Table does not enter in number of sections and in number of rows

I am using tableView but it does not enter in number of sections and number of rows in tableView here is the code
here is the code for number of sections and number of rows.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
Number of Rows in Sections Code
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(#"Cell is");
return 1;
}
Cell Values
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
}
MultipleDetailViewsWithNavigatorAppDelegate *appDelegate = (MultipleDetailViewsWithNavigatorAppDelegate *)[[UIApplication sharedApplication] delegate];
cell.textLabel.font=[UIFont fontWithName:#"Helvetica" size:16];
cell.textLabel.textAlignment=UITextAlignmentLeft;
PublishData *theCellData = [appDelegate.publishArray objectAtIndex:indexPath.row];
NSLog(#"Cell is");
cell.textLabel.text =#"Test";
return cell;
}
Have you set datasource and delegate for the table view? I suspect not. Please set it in code or xib and check.
How you are have added table view ?. if you have added using interface builder then set your datasoure for the tableview. If you have added pro-grammatically then set [tableview setDatasoucre:self];

How to Select & checkmark the row after load in UITableview

I have a selection that will select just 1 row of category.
but I will like it to select Spirits row when loaded.
Something like this:
At the moment it comes to this without selecting anything:
Where shall I do the compare for the in order for it to selectRowAtIndexPath?
// 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] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
categoryString = [arrayCategory objectAtIndex:indexPath.row];
cell.textLabel.text = categoryString;
if (cell.textLabel.text == categoryString) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell* newCell = [tableView cellForRowAtIndexPath:indexPath];
int newRow = [indexPath row];
int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
if(newRow != oldRow)
{
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
newCell.highlighted =YES;
UITableViewCell* oldCell = [tableView cellForRowAtIndexPath:lastIndexPath];
oldCell.accessoryType = UITableViewCellAccessoryNone;
lastIndexPath = indexPath;
oldCell.highlighted = NO;
}
[tableView deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:YES];
}
Use selectRowAtIndexPath:animated:scrollPosition: method to select a row programatically.
Few things to correct in your code. You should use isEqualToString: method to match the strings, like this, if ([cell.textLabel.text isEqualToString:categoryString]) {. Next thing is, you are assigning the categoryString to cell.textLabel.text, and on the next line you are matching them, so it would always return YES. I think you are matching the wrong values.
You have to manually set multiple check marks. for that you can use UIImageview for each UITableViewCell and as per the stored old data, you have to set check marks in didSelectRowAtIndexPath(delegate method of UITableView).

TalbeView cell showing null value

I have created a custom table for the rootViewController of a split view application so that the selected row expands and shows as a sub-table (showing main menu and sub menu ). The first row of the sub-table should show the particular main menu item of the sub-table.I'm fetching the main menu items from another class.
The problem is that the first row for the sub table is showing blank for me.Using NSLog, I checked the value of variable just before assigning it to the cell and even after assigning the value to the cell, i checked the text value in the cell using cell.textLabel.text. I'm getting the value in the console every time, but the row is still blank!!!
Row is showing the value if I'm hard coding it with any value!!!
Note:TableView is showing values for remaining rows.
Anybody can help me?? Thanks in advance...and sorry for my poor English..
EDIT: In the rootViewController:
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
tableView.separatorColor=[UIColor grayColor];
if (sectionopen[indexPath.row]) {
accordianTable *cell1;
cell1=(accordianTable *)[tableView dequeueReusableCellWithIdentifier:#"cell1"];
if (cell1 == nil) {
cell1 = [[[accordianTable alloc] initWithFrame:CGRectZero reuseIdentifier:#"cell1"] autorelease];
}
cell1.selectionStyle=UITableViewCellSelectionStyleNone;
return cell1;
} else {
//tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
static NSString *CellIdentifier = #"CellIdentifier";
// Dequeue or create a cell of the appropriate type.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.selectionStyle = UITableViewCellSelectionStyleGray;
}
// Configure the cell.
cell.textLabel.text=[UIAppDelegate.mainMenu objectAtIndex:indexPath.row];
return cell;
}
}
(void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
accordianTable *acc = [[accordianTable alloc]init];
acc.titl=[UIAppDelegate.mainMenu objectAtIndex:indexPath.row];
[acc.subTable reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
///turn them all off
sectionopen[0]=NO;
sectionopen[1]=NO;
sectionopen[2]=NO;
sectionopen[3]=NO;
///open this one
sectionopen[indexPath.row]=YES;
///animate the opening and expand the row
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
UIViewController *localdetailViewController = nil;
}
In the custom cell class (accordianTable):
(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Number of rows is the number of time zones in the region for the specified section.
return [UIAppDelegate.subMenu count]+1;//including title and sub menu
}
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = #"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
}
switch (indexPath.row)
{
case 0:
NSLog(#"text is >> %#",titl);
cell.textLabel.text=titl;
NSLog(#"text is >> %#",cell.textLabel.text);
cell.textLabel.textColor=[UIColor whiteColor];
cell.contentView.backgroundColor=[UIColor blackColor];
cell.textLabel.backgroundColor=[UIColor blackColor];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
break;
default:
int Row=indexPath.row;
Row--;
cell.textLabel.text=[UIAppDelegate.subMenu objectAtIndex:Row];
cell.textLabel.textColor=[UIColor orangeColor];
cell.textLabel.textAlignment=UITextAlignmentCenter;
cell.selectionStyle=UITableViewCellSelectionStyleNone;
break;
}
return cell;
}

Section receiving cell values of other section

I am trying to get the first five cards in one section and other five in next section of table view . Actually I have done parsing and unable to get proper cell values. The thing I am getting is combined values for example : in section = birthday cell values are card1, card2 and again card1, card 2 of new year section, and in other section same thing is repeating. What should I do to make grouped i.e card1, card 2 in birthday section and card1, card2 should be in new year section. Here is my code:
(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
NSLog(#"section in rv");
}
(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [appDelegate.books count];
NSLog(#".....appDelegate.books count");
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"cell for index path");
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
Book *aBook = [appDelegate.books objectAtIndex:indexPath.row];
NSLog(#"text for cell...");
cell.text = aBook.Subcatname;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
[appDelegate release];
}
Take a look at dks1725's response to set up the correct number of rows per section.
Also, and more directly in response to your question, in tableView:cellForRowAtIndexPath: for each table row you are always returning a cell with the same content, regardless of what section the cell is in. That's why you are getting the same content ('card1', 'card2' and so on) appearing in each section.
Keep in mind that the indexPath you are receiving in this method has two components: indexPath.rowand indexPath.section. You are ignoring the second of these, and so, for every section, you always return the same cell for a each row.
You will need to modify tableView:cellForRowAtIndexPath: so that a piece of it will look something like the following:
if (indexPath.section == 0) {
//Retrieve the content you want for each row in section 0
//It will look something like this, but will be different depending on where you stored your content
Book *aBook = [appDelegate.books objectAtIndex:indexPath.row];
cell.text = birthdayBook.Subcatname;
}
if (indexPath.section == 1) {
//Retrieve the content you want for each row in section 1
//It will look something like this, but will be different depending on where you stored your content
Book *aBook = [appDelegate.books objectAtIndex:indexPath.row + 5];
cell.text = newyearsBook.Subcatname;
}
...
The above is edited as per your comments.
Given your model, you could also do it in fewer lines, as follows:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;}
-(NSString *)tableView:(UITableView *)tblView titleForHeaderInSection:(NSInteger)section {
NSString *sectionName = nil;
switch(section)
{
case 0:
sectionName = [NSString stringWithString:#"birthday"];
break;
case 1:
sectionName = [NSString stringWithString:#"new year"];
}
return sectionName;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSInteger count;
if(section == 0)
{ count=5; }
else if(section == 1)
{ count=5; }
return count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"cell for index path");
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
NSLog(#"text for cell...");
if(indexPath.section == 0)
{
Book *aBook = [appDelegate.books objectAtIndex:indexPath.row];
cell.text = aBook.Subcatname;
}
else if(indexPath.section == 1)
{
Book *aBook = [appDelegate.books objectAtIndex:indexPath.row+5*indexPath.section];
cell.text = aBook.Subcatname;
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
[appDelegate release];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic -- create and push a new view controller
if(bdvController == nil)
bdvController = [[BookDetailViewController alloc] initWithNibName:#"BookDetailView" bundle:[NSBundle mainBundle]];
NSLog(#"pushing to bdv controller");
if(indexPath.section == 0)
{
Book *aBook = [appDelegate.books objectAtIndex:indexPath.row];
bdvController.aBook = aBook;
}
else if(indexPath.section == 1)
{
Book *aBook = [appDelegate.books objectAtIndex:indexPath.row+5*indexPath.section];
bdvController.aBook = aBook;
}
NSLog(#"push to view descrip, id, pic url");
//bdvController.aBook = aBook;
NSLog(#"loop");
[self.navigationController pushViewController:bdvController animated:YES];
}
You have to set number of rows for each section something like below..
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(indexPath.section==0)
//return no of rows for section 0
else
//return no of rows for section 1
}

Problem implementing a UITableView that allows for multiple row selections

This
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
... modifies the "accessoryType" of every 6th cell INSTEAD of just the selected row. What am I missing?
Thanks
UPDATED: Here is the cell creation code ...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *TC = #"TC";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: TC];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:PlayerTableViewCell] autorelease];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [NSString stringWithFormat:#"Person %d", row+1];
return cell;
}
MY SOLUTION based on the marked answer below is ...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// EVERY row gets its one Identifier
NSString *TC = [NSString stringWithFormat: #"TC%d", indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: TC];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TC] autorelease];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [NSString stringWithFormat:#"Person %d", row+1];
return cell;
}
If there is a better way I'm all ears. Would be nice if we could just change the SPECIFIC Cell according to the NSIndexPath passed in someday (at least that seems a whole lot more intuitive to me).
I got it to work by using setting the accessory type in cellForRowAtIndexPath based on variable which keeps track of the selected row. Then in didSelectRowAtIndexPath i just deselect the row, update the variable, and reload the table.
CODE SAMPLE:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [NSString stringWithFormat:#"%d", indexPath.row];
if ([_cells indexOfObjectIdenticalTo:_selectedCell] == indexPath.row) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:NO];
_selectedCell = [_cells objectAtIndex:indexPath.row];
[tableView reloadData];
}
Yeah, just figured out. These are the same cells ( logs below are NSLog( #"%d %#", row, cell ) ).
2010-04-15 12:43:40.722 ...[37343:207] 0 <MyListCell: 0x124bee0; baseClass = UITableViewCell; frame = (0 0; 320 44); autoresize = RM+TM; layer = <CALayer: 0x124c3a0>>
2010-04-15 12:43:47.827 ...[37343:207] 10 <MyListCell: 0x124bee0; baseClass = UITableViewCell; frame = (0 31; 340 44); autoresize = W; layer = <CALayer: 0x124c3a0>>
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: PlayerTableViewCell];
dequeueReusableCellWithIdentifier uses already created cell, if the previous one is out of a screen.
So, you should avoid using "dequeueReusableCellWithIdentifier" - create a dictionary NSIndexPath -> UITableViewCell and use it instead of dequeueReusableCellWithIdentifier.
ADDED:
You can also check if the (rowNumber+1) exists in a winningNumbers array and replace accessory in a tableView:cellForRowAtIndexPath: method. This should work too