I have a viewcontroller with a nib - which has a tableview, searchbar and searchdisplaycontroller inside.
The searchdisplaycontroller's contentscontroller, results delegate, delegate and results datasource is wired to the viewcontroller in IB. The searchbar's delegate is wired to the view controller in IB. The main tableview is also wired to the viewcontroller and its loading items during runtime properly.
Everything seems to work except the resultstableview - it never gets populated and the tableView:cellForRowAtIndexPath: never gets called AFTER the user types into the searchbar - it only gets called to populate the main tableview.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CellIdentifier";
// Dequeue or create a cell o f the appropriate type.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
if (tableView.tag == 2)
{
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
// Configure the cell.
NSHotel *selectedHotel = [[self hotelList] objectAtIndex:[indexPath row]];
//Store the hotel id for use later
selectedHotel.id = [indexPath row];
//cell.textLabel.text = [NSString stringWithFormat:#"Catalonia Majorica", indexPath.row];
cell.textLabel.text = [selectedHotel name];
}
else if (tableView.tag == 1)
{
cell.accessoryType = UITableViewCellAccessoryNone;
// Configure the cell...
NSAirport *selectedAirport = nil;
if (tableView == self.searchDisplayController.searchResultsTableView)
{
selectedAirport = [self.filteredListContent objectAtIndex:indexPath.row];
}
else
{
selectedAirport = [self.listContent objectAtIndex:indexPath.row];
}
//Set the label of the cell formatting the distance from the device
cell.textLabel.text = [NSString stringWithFormat:#"%# - %# miles",
selectedAirport.name, [self formattedStringWithDecimal:selectedAirport.milesFromDevice]];
//Adjust the front of the label
cell.textLabel.font = [UIFont boldSystemFontOfSize:12];
}
return cell;
}
Fixed it - it was returning 0 here:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.searchDisplayController.searchResultsTableView)
{
return [self.filteredListContent count];
}
else if (tableView.tag == 1)
{
return [self.listContent count];
}
else if (tableView.tag == 2)
{
// Return the number of rows in the section.
return [[self hotelList] count];
}
NSLog(#"The rows in tableview!");
return 0;
}
Related
Trying to expand the sections each section will have two rows when expanded. It is giving crash. Below is code am trying.
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
return [self.mArrQues count];
}
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(!helpOn)
return 1;
else
if(section == selectedCellIndexPath)
{
return 2;
}
else{
return 1;
}
return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"CellIdentifier";
UITableViewCell *cell;
cell = [self.mHelpTable dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
else{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
UIView* myBackgroundView = [[UIView alloc] initWithFrame:CGRectZero];
myBackgroundView.backgroundColor = [UIColor colorWithRed:240.0/255.0 green:240.0/255.0 blue:240.0/255.0 alpha:1.0];
cell.backgroundView = myBackgroundView;
//[cell addSubview:myBackgroundView];
if(!helpOn)
{
cell.textLabel.text = [self.mArrQues objectAtIndex:indexPath.section];
}
else
{
if(indexPath.row == 0)
{
cell.textLabel.text = [self.mArrQues objectAtIndex:indexPath.section];
}
else{
cell.textLabel.text = [self.mArrAns objectAtIndex:indexPath.section];
}
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
helpOn = !helpOn;
int ind = indexPath.section;
if(ind == selectedCellIndexPath)
{
}
else{
helpOn = 1;
}
if(helpOn)
{
selectedCellIndexPath = indexPath.section;
[self.mHelpTable reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade];
}
else
{
if(indexPath.row == 0)
{
//selectedCellIndexPath = indexPath.section;
[self.mHelpTable reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade];
}
}
}
Error :
Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (2) must be equal to the number of rows contained in that section before the update (1), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'
As you are changing the number of cells at section, you need to wrap the reload operation with beginUpdates and endUpdates as well as specify the cells insertion and removal with insertRowsAtIndexPaths:withRowAnimation and deleteRowsAtIndexPaths:withRowAnimation:.
Alternatively you can use 'reloadData` to reload all the cells, however there will be problems either with animation or user experience if you use it.
Remove this from your code.
else{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
There is no need to alloc your cell if it is nil, tableview will reuse from previous cell in this line
cell = [self.mHelpTable dequeueReusableCellWithIdentifier:cellIdentifier];
AND
Make in .h
int selectedCellIndexPath;
BOOL selected;
in .m
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section==selectedCellIndexPath)
{
//selected > this is because initialy selectedCellIndexPath is 0 and section is also 0
if (selected) {
return 2;
}
else{
return 1;
}
}
else{
return 1;
}
return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"CellIdentifier";
UITableViewCell *cell;
cell = [self.mHelpTable dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
UIView* myBackgroundView = [[UIView alloc] initWithFrame:CGRectZero];
myBackgroundView.backgroundColor = [UIColor colorWithRed:240.0/255.0 green:240.0/255.0 blue:240.0/255.0 alpha:1.0];
cell.backgroundView = myBackgroundView;
//[cell addSubview:myBackgroundView];
//if(!helpOn)
if (indexPath.section!=selectedCellIndexPath)
{
cell.textLabel.text = [mArrQues objectAtIndex:indexPath.section];
}
else
{
if(indexPath.row == 0)
{
cell.textLabel.text = [mArrQues objectAtIndex:indexPath.section];
}
else{
cell.textLabel.text = [mArrAns objectAtIndex:indexPath.section];
}
}
//cell.textLabel.text = [self.mArrQues objectAtIndex:indexPath.section];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
selected= !selected;
selectedCellIndexPath = indexPath.section;
//selectedCellIndexPath = indexPath.row;
[self.mHelpTable reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade];
}
Hi I'm creating an app related to iOS default alarm. I'm selecting a multiple cell in tableView. If I click the back button the selected cells are displayed in tableViewCell as a label in another view. I'm Using story board. How to do this?
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *thisCell = [tableView cellForRowAtIndexPath:indexPath];
if (thisCell.accessoryType == UITableViewCellAccessoryNone) {
thisCell.accessoryType = UITableViewCellAccessoryCheckmark;
[myIndexArray addObject:[NSString stringWithFormat:#"%d",indexPath.row]];
}
else
{
thisCell.accessoryType = UITableViewCellAccessoryNone;
for(int i=0; i<myIndexArray.count; i++)
{
if([[myIndexArray objectAtIndex:i]intValue]== indexPath.row)
{
[myIndexArray removeObjectAtIndex:i];
break;
}
}
}
}
The sample image I want like this
I want something like a Repeat View and press back button the selected cells are displayed in Repeat tableViewCell.
I did the same in my app. I can help you...
RepeatDayViewController
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (UITableViewCellAccessoryNone == cell.accessoryType )
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
NSNumber *dayNumber = [NSNumber numberWithInteger:indexPath.row];
[self.repeatDays addObject:dayNumber];
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
NSNumber *dayNumber = [NSNumber numberWithInteger:indexPath.row];
[self.repeatDays removeObject:dayNumber];
}
}
I am passing self.repeatDays to AddAlarmViewController
In
AddAlarmViewController
I have an array like this
_days = [[NSArray alloc ]initWithObjects:#"Sun",#"Mon",#"Tue",#"Wed",#"Thu",#"Fri",#"Sat",nil];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([self.repeatDays count])
{
NSMutableArray *repeatDays = [NSMutableArray array];
for (NSNumber *dayNumber in self.repeatDays)
{
[repeatDays addObject:[_days objectAtIndex:[dayNumber integerValue]]];
}
NSString *repeatLabel = [repeatDays componentsJoinedByString:#" "];
cell.detailTextLabel.text = repeatLabel;
}
else
{
cell.detailTextLabel.text = NSLocalizedString(#"Never",nil);
}
}
Where ever the user select a row save the action any ware such as NSUserDefaults
in the
-(void)viewDidAppear:(BOOL)animated{
UITableViewCell* testCell = [azanTableview cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
if ([[NSUserDefaults standardUserDefaults]boolForKey:#"testCell"]==YES) {
testCell.accessoryType = UITableViewCellAccessoryCheckmark;
testCell .textLabel.textColor = [UIColor colorWithRed:0.22 green:0.33 blue:0.53 alpha:1.0];
}
}
And so on...
I'm trying to implement a searchDisplayController but I've a problem. Once I filtered the cell and when I selected the cell remaining I get a strange stack of text :
http://postimage.org/image/4fswe8t8n/
It's like having all my cell in one.
So there's my cellForRowAtIndexPath method :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"MFriendCell";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if(tableView == self.tableView) {
UILabel *nameLabel = (UILabel *)[cell viewWithTag:201];
nameLabel.text = [self.allItems objectAtIndex:indexPath.row];
}
else if(tableView == self.searchDisplayController.searchResultsTableView) {
UILabel *labelName = [ [UILabel alloc ] initWithFrame:CGRectMake(50, 0.0, 150.0, 43.0) ];
labelName.text = [self.searchResults objectAtIndex:indexPath.row];
[cell addSubview:labelName];
}
return cell;
}
And my filter :
- (void)filterContentForSearchText:(NSString*)searchText
scope:(NSString*)scope
{
NSPredicate *resultPredicate = [NSPredicate
predicateWithFormat:#"SELF contains[cd] %#",
searchText];
self.searchResults = [self.allItems filteredArrayUsingPredicate:resultPredicate];
}
if you need anything else just ask :)
You can make label in you tableView like this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"MFriendCell";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
UILabel *nameLabel = (UILabel *)[cell viewWithTag:201];
if(tableView == self.tableView)
{
nameLabel.text = [self.allItems objectAtIndex:indexPath.row];
}
else if(tableView == self.searchDisplayController.searchResultsTableView)
{
labelName.text = [self.searchResults objectAtIndex:indexPath.row];
}
return cell;
}
Just add nameLabel in cell same like labelName see the code bellow..
if(tableView == self.tableView) {
UILabel *nameLabel = (UILabel *)[cell viewWithTag:201];
nameLabel.text = [self.allItems objectAtIndex:indexPath.row];
[cell addSubview:nameLabel]; ///YOU ARE Not add This Lable so Add this lable also
}
else if(tableView == self.searchDisplayController.searchResultsTableView) {
UILabel *labelName = [ [UILabel alloc ] initWithFrame:CGRectMake(50, 0.0, 150.0, 43.0) ];
labelName.text = [self.searchResults objectAtIndex:indexPath.row];
[cell addSubview:labelName];
}
here in your didSelectRowAtIndexPath method u just take value of self.allItems array instead of self.searchResults array WHEN you are in searchResultTableView
try this code
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;{
if(tableView == self.tableView) {
NSLog("\n Normal Table View Selected cell ==> %#",[self.allItems objectAtIndex:indexPath.row]);
}
else if(tableView == self.searchDisplayController.searchResultsTableView) {
NSLog("\n Search TableView Selected cell ==> %#",[self.searchResults objectAtIndex:indexPath.row];);
}
}
and also in numberOfRowsInSection method set row like bellow...
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if(tableView == self.tableView) {
return [self.allItems count];
}
else{
return [self.searchResults count];
}
}
i hope this help you...
I have one tableview with 1 section and two rows,
and down I have one button action.
In the button action I have to check which cell is clicked because according to that cell I have to open email view for sending mail.
I created for two bool values, but It is not working properly, still my emailview get call without cell is selected
this code what i am doing
#implementation View
#synthesize Mytableview,selectedIndexPaths,value1,value2,cellselected;
BOOL values[1];
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [Mytableview dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
if ([indexPath row] == 0 && [indexPath section] == 0)
{
cell.textLabel.text= #"test";
if (values[indexPath.row]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
value1=TRUE;
}
else if ([indexPath row] == 1 && [indexPath section] == 0)
{
cell.textLabel.text= #"test";
value2=TRUE;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//[Mytableview deselectRowAtIndexPath:indexPath animated:YES];
//self.cellselected = indexPath.row;
UITableViewCell *cell = [Mytableview cellForRowAtIndexPath:indexPath];
//here values is i give as array on top as BOOL values[1];i give size for it
values[indexPath.row] = !values[indexPath.row];
if (values[indexPath.row]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
-(IBAction)ExportEmail
{
if (value1==FALSE) {
return;
}
else {
Class mailClass = (NSClassFromString(#"MFMailComposeViewController"));
if (mailClass != nil)
{
// We must always check whether the current device is configured for sending emails
if ([mailClass canSendMail])
{
[self displayComposerSheet];
}
}
}
}
Declare class veriable NSIndexPath;
NSIndexPath *myIndexPath;
Modify your didSelectRowAtIndexPath to below
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [Mytableview cellForRowAtIndexPath:indexPath];
//here values is i give as array on top as BOOL values[1];i give size for it
myIndexPath = indexPath;
if (values[indexPath.row]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
You can use that NSIndexPath in you Button's IBAction and according to index you can open up email.
-(IBAction)ExportEmail
{
if(myIndexPath.row==0){
// send mail
}else if(myIndexPath.row==1){
// send mail
}
}
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
}