cellForRowAtIndexPath ignores sections - objective-c

in my UITableViewController I defined 3 sections. But if I want to access more sections in the cellForRowAtIndexPath method than the first one, this is ignored. For example, if I want to add a row in the second section. Does anyone have a solution to the problem?
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0) {
if (indexPath.row == 0) {
..
return cell;
}
if (indexPath.row == 1) {
..
return cell;
}
if (indexPath.row == 2) {
..
return cell;
}
}
if (indexPath.section == 1) {
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:aufgabe];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:aufgabe];
}
[cell.textLabel setText:#"Test"];
}
}
else if (indexPath.section == 2) {
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:geraet];
return cell;
}
return nil;
}

Funny! I fixed it for myself. I had to fill the numberOfRowsInSection method.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 0) {
return 3;
}
if (section == 1) {
return 5;
}
else {
return 0;
}
}

Related

want to check only one tablecell at a time in TableView

In the source code below I am able to change my checkmark object value, but at the time of reload data in cellForRowAtIndexPath method it shows old data:
//TableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section
{
return [self.tableData count];
}
//CellforRowAtIndexpath showing old table data
- (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];
}
Recipe *obj = nil;
obj = self.tableData[indexPath.row];
cell.textLabel.text = obj.name;
NSLog(#"Reload Data %#", obj.checkmark);
if ([obj.checkmark integerValue] == 1)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Recipe *recipe = nil;
recipe = [self.tableData objectAtIndex:indexPath.row];
if ([recipe.checkmark integerValue] == 1)
{
recipe.checkmark=#"0";
}
else
{
//recipe.checkmark=#"1";
for (int i=0; i<[self.tableData count]; i++) {
if (i==indexPath.row) {
recipe.checkmark=#"1";
}
else{
recipe.checkmark=#"0";
}
NSLog(#"PK ! %#", recipe.checkmark);
}
}
//[self.tableData addObject:recipe.checkmark];
[self.myTable reloadData];
[self.myTable reloadData];
}
The good news is normally this issue is in the cell for row logic, but it looks like you have that portion right. I believe the issue is in your cell for row logic because you are not updating tableData correctly. See extra comments below.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Recipe *recipe = nil;
recipe = [self.tableData objectAtIndex:indexPath.row];
if ([recipe.checkmark integerValue] == 1)
{
recipe.checkmark=#"0";
}
else
{
//This only gets called if current checked cell is already checked
for (int i=0; i<[self.tableData count]; i++) {
if (i==indexPath.row) {
recipe.checkmark=#"1";
}
else{
//This is still the recipe for the cell currently selected
recipe.checkmark=#"0";
}
NSLog(#"PK ! %#", recipe.checkmark);
}
}
//[self.tableData addObject:recipe.checkmark];
[self.myTable reloadData];
[self.myTable reloadData];
}
Something like this should fix your issue without changing too much of your code or structure.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Recipe *recipe = [self.tableData objectAtIndex:indexPath.row];
if ([recipe.checkmark integerValue] == 1)
{
recipe.checkmark=#"0";
}
else
{
recipe.checkmark=#"1";
}
//remove all old checkmarks
for (int i=0; i<[self.tableData count]; i++)
{
//Only update recipes that are not in this row
if (i!=indexPath.row)
{
recipe = [self.tableData objectAtIndex:i];
recipe.checkmark=#"0";
}
}
[self.myTable reloadData];
}
Another things to consider is instead of using a string that you are converting to an integer that is acting like a BOOL just change recipe's property checkmark to a BOOL. I hope that helps and good luck.

UISearchDisplayController Not Displaying Results in searchResultsTableView

I have a UISearchDisplayController properly connected to the header of a custom UITableView in IB. However, when I search for anything the searchResultsTableView only displays "No Results", and I cannot seem to find where the code is incorrect.
searchResults Property
- (NSMutableArray *)searchResults {
if (!_searchResults) {
_searchResults = [[NSMutableArray alloc] initWithCapacity:self.listingPreviews.count];
}
return _searchResults;
}
Table View Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
NSInteger numberOfRows = 0;
if (tableView == self.tableView) {
numberOfRows = self.listingPreviews.count;
} else {
numberOfRows = self.searchResults.count;
}
return numberOfRows;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Listing";
ListingPreviewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[ListingPreviewTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell.
if (tableView == self.tableView) {
cell.listingPreview = [self.listingPreviews objectAtIndex:indexPath.row];
} else {
NSLog([[self.searchResults objectAtIndex:indexPath.row] title]);
cell.listingPreview = [self.searchResults objectAtIndex:indexPath.row];
}
return cell;
}
Search Bar Delegate Method
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
NSInteger searchTextLength = searchText.length;
for (ListingPreview *listingPreview in self.listingPreviews) {
if ((listingPreview.title.length >= searchTextLength) && ([listingPreview.title rangeOfString:searchText].location != NSNotFound)) {
[self.searchResults addObject:listingPreview];
}
}
}
Just try this instead:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Listing";
ListingPreviewTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell.
if (tableView == self.tableView) {
cell.listingPreview = [self.listingPreviews objectAtIndex:indexPath.row];
} else {
NSLog([[self.searchResults objectAtIndex:indexPath.row] title]);
cell.listingPreview = [self.searchResults objectAtIndex:indexPath.row];
}
return cell;
}
Please note that the change was applied on:
ListingPreviewTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
This way you deque a Cell from your own tableView and not from the one created by UISearchDisplayController.
It seems that nothing in your code forces searchDisplayController to reload it's searchResultTableView.
The standard approach is to set UISearcDisplayController's delegate (note the protocol - UISearchDisplayDelegate) and implement – searchDisplayController:shouldReloadTableForSearchString:
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
// perform your search here and update self.searchResults
NSInteger searchStringLength = searchString.length;
for (ListingPreview *listingPreview in self.listingPreviews) {
if ((listingPreview.title.length >= searchStringLength) && ([listingPreview.title rangeOfString:searchString].location != NSNotFound)) {
[self.searchResults addObject:listingPreview];
}
}
// returning YES will force searchResultController to reload search results table view
return YES;
}

Display multiple custom cells in a UITableView?

I am using Xcode 4.2 on SnowLeopard, and my project is using storyboards. I am trying to implement a UITableView with 2 different custom cell types, sessionCelland infoCell. I can get the 2 types to appear within the same list, but now I have a new problem?! The sessionCell is displayed once, and then X number of infoCells are displayed after it - just as I wanted - except that the first infoCell is always overwritten by the sessionCell!
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return [self.people count];
}
//inside cellForRowAtIndexPath
if(indexPath.row == 0) {
cell = [tableView dequeueReusableCellWithIdentifier:#"sessionCell"];
} else {
cell = [tableView dequeueReusableCellWithIdentifier:#"infoCell"];
}
...
return cell;
I've tried to say return array count + 1, or even hardcoded return 7 (it fits my example) but both are incorrect!
myObject *person = [self.people objectAtIndex:indexPath.row];
Or does my problem lie in the above line? I've even tried indexPath.row+1...
Any help would be greatly appreciated!!
If I understand your question correctly, the first infoCell (second UITableView row) should display the first person object's data, right?
Then it seems you want:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *sessionCellID = #"sessionID";
static NSString *infoCellID = #"infoID";
if( indexPath.row == 0 ) {
SessionCellClass *cell = nil;
cell = (SessionCellClass *)[tableView dequeueReusableCellWithIdentifier:sessionCellID];
if( !cell ) {
// do something to create a new instance of cell
// either alloc/initWithStyle or load via UINib
}
// populate the cell with session model
return cell;
}
else {
InfoCellClass *cell = nil;
cell = (InfoCellClass *)[tableView dequeueReusableCellWithIdentifier:infoCellID];
if( !cell ) {
// do something to create a new instance of info cell
// either alloc/initWithStyle or load via UINib
// ...
// get the model object:
myObject *person = [[self people] objectAtIndex:indexPath.row - 1];
// populate the cell with that model object
// ...
return cell;
}
}
and you need to return [[self people] count] + 1 for the row count:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[self people] count] + 1;
}
so that the n'th row shows the (n-1)th data.
if you look at the if else section it shows that the first row is an "sessionCell" and all other rows are "infoCells" what i think you want to do is make the first row an sessionCell, the second row an infoCell and all of the other rows peopleCells
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.people count] + 2; // Added two for sessionCell & infoCell
}
//inside cellForRowAtIndexPath
if(indexPath.row == 0) {
cell = [tableView dequeueReusableCellWithIdentifier:#"sessionCell"];
} else if (indexPath.row == 1 {
cell = [tableView dequeueReusableCellWithIdentifier:#"infoCell"];
} else {
cell = [tableView dequeueReusableCellWithIdentifier:#"personCell"];
Person *person = [self.people objectAtIndex:index.path.row - 2];
}
...
return cell;
Better yet, I would try to make two different sell sections, one for info and one for people
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return section == 0 ? 2 : self.people.count
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell = nil;
if (indexPath.section == 0) {
if(indexPath.row == 0) {
cell = [tableView dequeueReusableCellWithIdentifier:#"sessionCell"];
// configure session cell
} else if (indexPath.row == 1 {
cell = [tableView dequeueReusableCellWithIdentifier:#"infoCell"];
// configure info cell
}
} else {
cell = [tableView dequeueReusableCellWithIdentifier:#"infoCell"];
Person *person = [self.people objectAtIndexPath:indexPath.row];
// configure person cell
}
return cell;
}

how to customize the tableview has shown in figure

How to create the tableview has shown below picture
.
If i created section i am missing th boarder line what to do ?
You need to set the UITableView to Grouped Style. Then you need 2 Sections, with no Header.
also you have to set the
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
Example Code:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
if(section == 0)
return 1;
else
return 2;
}
- (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...
switch(indexPath.section)
{
case 0:
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text = #"Create a Holiday Card";
break;
case 1:
switch(indexPath.row)
{
case 0:
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text = #"Login to Photo";
break;
case 1:
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text = #"Create a Free Account";
break;
}
}
return cell;
}
Per example above
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;// Amount of sections
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(section == 0){
return 1;
}
else if (section == 1){
return 2;
}
}
- (void)tableViewUITableView *)aTableView didSelectRowAtIndexPathNSIndexPath *)indexPath
{
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
I assume you are referring to the STYLE of that example.
[self.view setBackgroundColor: [UIColor blueColor]];
/* also assuming your table sits on a UIViewController */
[myTableView setBackgroundColor: [UIColor clearColor]];
[myTableView setOpaque: NO];

Cells reorganizing themselves on iPhone?

I'm writing an app for my father's business, it involves lists, however, I have seen that it seems to reorganize the list, even though I don't want it to. For example, all of section 1 is supposed to say one thing, when I scroll down, content from the other sections is put in section 1's cells, this goes for the other sections as well.
Here's all the table code, if it helps.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == ENGINEER_ID) {
return 3;
} else if (section == FIREMAN_ID) {
return 3;
} else if (section == CONDUCTOR_ID) {
return 3;
} else if (section == TRAINMASTER_ID) {
return 3;
} else {
return 0;
}
}
// 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:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
if ([indexPath section] == ENGINEER_ID) {
cell.textLabel.text = #"Engineer";
} else if ([indexPath section] == FIREMAN_ID) {
cell.textLabel.text = #"Fireman";
} else if ([indexPath section] == CONDUCTOR_ID) {
cell.textLabel.text = #"Conductor";
} else if ([indexPath section] == TRAINMASTER_ID) {
cell.textLabel.text = #"Trainmaster";
}
}
// Configure the cell.
return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (section == ENGINEER_ID) {
return #"Master Engineer II";
} else if (section == FIREMAN_ID) {
return #"Fireman";
} else if (section == CONDUCTOR_ID) {
return #"Conductor";
} else if (section == TRAINMASTER_ID) {
return #"Trainmaster";
} else {
return #"What.";
}
}
You need to move the assignment of text out of the conditional.
After the first 3 cells's are alloc/init'd, you won't necessarily be getting more cells.
You are assigning cell.textLabel.text only if you create a new cell.
Logic should be:
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
if ([indexPath section] == ENGINEER_ID) {
cell.textLabel.text = #"Engineer";
} else if ([indexPath section] == FIREMAN_ID) {
cell.textLabel.text = #"Fireman";
} else if ([indexPath section] == CONDUCTOR_ID) {
cell.textLabel.text = #"Conductor";
} else if ([indexPath section] == TRAINMASTER_ID) {
cell.textLabel.text = #"Trainmaster";
}
And of course after you tire of writing cascading if statements, you could use switch([indexPath section])