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])
Related
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;
}
}
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;
}
I created a uitableview and when i launch it, it looks as it is supposed to, but when I scroll, it puts text in all the other sections that I don't specify, can someone please help.
The first image attached is how it should look. The second it what it does when I scroll.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 0 ){
return 1;}
else if (section == 1){
return [cellLabels count];
}else if (section == 2){
return 1;
}else{
return 0;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"cellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
tableView.showsVerticalScrollIndicator = NO;
// cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
if( indexPath.section == 0 )
{
}
else if (indexPath.section == 1)
{
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor = [UIColor blackColor];
// headerLabel.font = [UIFont SystemFontOfSize:16];
[cell.textLabel setFont:[UIFont fontWithName:#"Arial" size:14]];
cell.textLabel.text = [cellLabels objectAtIndex:indexPath.row];
}
return cell;
}
your problem is pretty simple ,
since table view reuses allocated cells,
when it comes to first time to first section your displaying nothing , in second section displaying your custom texts
when it scrolls down and come back it text will appear in first section because when it reaches
if( indexPath.section == 0 )
{
}
it wont do anything so
make it
if( indexPath.section == 0 )
{
cell.textLabel.text = #"";
}
else if( indexPath.section == 2 )
{
cell.textLabel.text = #"";
}
or
if( indexPath.section == 0 )
{
cell.textLabel.text = nil;
}
else if( indexPath.section == 2 )
{
cell.textLabel.text = nil;
}
other FOR SECTION 1 is correct
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];
when i build and analyze, i am told that cell never gets a value...which seems false by my logic, but then the app crashes trying to load the table. so...why come?
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *Cell1Identifier = #"Cell1";
static NSString *Cell2Identifier = #"Cell2";
static NSString *Cell3Identifier = #"Cell3";
UITableViewCell *cell;
if ([indexPath section] == 0) {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Cell1Identifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2
reuseIdentifier:Cell1Identifier] autorelease];
}
}
else if ([indexPath section] == 1) {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Cell3Identifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:Cell3Identifier] autorelease];
}
}
else {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Cell2Identifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:Cell2Identifier] autorelease];
}
}
// Configure the cell...
[self configureCell:cell
atIndexPath:indexPath];
return cell;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *Cell1Identifier = #"Cell1";
static NSString *Cell2Identifier = #"Cell2";
static NSString *Cell3Identifier = #"Cell3";
NSString *identityString = #"";
switch ([indexPath section]) {
case 0: {
identityString = Cell1Identifier;
break;
}
case 1: {
identityString = Cell3Identifier;
break;
}
case 2: {
identityString = Cell2Identifier;
break;
}
default:
break;
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identityString];
if ([indexPath section] == 0) {
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2
reuseIdentifier:Cell1Identifier] autorelease];
}
}
else if ([indexPath section] == 1) {
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:Cell3Identifier] autorelease];
}
}
else {
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:Cell2Identifier] autorelease];
}
}
// Configure the cell...
[self configureCell:cell
atIndexPath:indexPath];
return cell;
}