How to get text from textView from custom cell iphone - iphone

I have a table view with custom cell.On my custom cell, I have a label and textView and I want to get data from textView to save in feedBack button. When I add txtView in my data array, I get custom cell twice. How to remove this issue
- (void)textViewDidEndEditing:(UITextView *)textView
{
FeedbackQuestionDC *feedBack = [dataArray objectAtIndex:textView.tag];
feedBack.FeedbackQuestionDC_Answers=textView.text;
[dataArray addObject:feedBack];
[myTableView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = #"Feed Back";
feedBackCC *cell = (feedBackCC *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
UIViewController *c = [[UIViewController alloc] initWithNibName:#"feedBackCC" bundle:nil];
cell = (feedBackCC *) c.view;
}
cell.textLabel.font = [UIFont boldSystemFontOfSize:15.0];
FeedbackQuestionDC *feedBack = [dataArray objectAtIndex:[indexPath row]];
cell.lblQuestion.text = feedBack.FeedbackQuestionDC_QuestionText;
cell.txtViewAnswer.tag=indexPath.row;
cell.txtViewAnswer.text=feedBack.FeedbackQuestionDC_Answers;
cell.txtViewAnswer.delegate=self;
return cell;
}

First get the cell at particular index and then get view from it and then textview
Use following function to get cell
[table cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]]

- (void)textViewDidEndEditing:(UITextView *)textView
{
FeedbackQuestionDC *feedBack = [dataArray objectAtIndex:textView.tag];
feedBack.FeedbackQuestionDC_Answers=textView.text;
[dataArray addObject:feedBack]; //REMOVE THIS LINE
[myTableView reloadData];
}
See the code above remove the line I suggested, you don't need to add the object again into array, it has already been updated using the reference of the object from dataArray.
Hope this helps..

// i think it will be usefull for you, try this
- (void)textViewDidEndEditing:(UITextView *)textView
{
feedBackCC *cellsuperView = (feedBackCC *)[textView superview];
nslog(#"%#",cellsuperView.txtViewAnswer.text);
}

Related

UITableView indexPathForSelectedRow return 0 (incorrect value)

I'm trying to make an upcoming events table in my app but when I select a row indexPathForSelectedRow always returns 0.
NSArray *upcommingTitle;
NSMutableArray *upcommingSubtitle;
NSMutableArray *upcommingTime;
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)datesTable numberOfRowsInSection:(NSInteger)section
{
return upcommingTitle.count;
}
-(UITableViewCell *)tableView:(UITableView *)datesTable cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"Cell";
DatesCustomCell *Cell = [datesTable dequeueReusableCellWithIdentifier:cellIdentifier];
if(!Cell)
{
Cell = [[DatesCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
Cell.DatesTitle.text = [upcommingTitle objectAtIndex:indexPath.row];
Cell.DatesSubtitle.text = [upcommingSubtitle objectAtIndex:indexPath.row];
Cell.DatesTime.text = [upcommingTime objectAtIndex:indexPath.row];
return Cell;
}
self.datesTable.dataSource = self;
self.datesTable.delegate = self;
View Did Load
upcommingTitle = [[NSMutableArray alloc] initWithObjects:#"Title1", #"Title2", nil];
upcommingSubtitle = [[NSMutableArray alloc] initWithObjects:#"Sub1", #"Sub2", nil];
upcommingTime = [[NSMutableArray alloc] initWithObjects:#"Date1", #"Date2", nil];
But the following always returns 0 resulting in the "test" label to be "Title1"
View Did Appear
_test.text = [upcommingTitle objectAtIndex:self.datesTable.indexPathForSelectedRow.row];
Thanks for you help
try to push your another view in your didSelect method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Here, indexpath returns the selection of cell's indexpath, so put your code here
_test.text = [upcommingTitle objectAtIndex:indexPath.row];
//then push your view
}
UPDATED : then use this code
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([[segue identifier]isEqualToString:#"pushView"])
{
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
PushingViewController *pushView = [segue destinationViewController];
pushView._test.text = [upcommingTitle objectAtIndex:indexPath.row];
}
}
(void)tableView:(UITableView *)datesTable didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
_test.text = [upcommingTitle objectAtIndex:indexPath.row];
}
Just put this code in your application.
Try not to deselect cell before presenting your seque
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//Call This Line
[self performSegueWithIdentifier:#"ShowMobileShopStore" sender:nil];
//Before This
[tableView deselectRowAtIndexPath:indexPath animated:true];
}
The initial question was: indexPathForSelectedRow return 0 (incorrect value),
Well, this is by-design, from the apple developer notes, the value returns an the IndexPath which can be NULL (is what you are seeing).
So, to do things right:
1) You need to test if the indexPathForSelectedRow is NOT NULL first
2) If it's not NULL then it would contain (.row) which would then give you the selected row-index, this starts from 0.
3) Please note that indexPathForSelectedRow.row would return ZERO in the case of indexPathForSelectedRow is NULL.
Here is the code that I've tested and works:
if (MyTableViewList.indexPathForSelectedRow)
{
long SelectedRow = MyTableViewList.indexPathForSelectedRow.row;
}
I hope this helps,

How to use UITextField to input information to UITableView?

I am trying to make a ToDo list app and I can't for the life of me seem to figure out how to make the text box's typed text go into a box with a tick box for me to be able to change to completed.
Can anyone help me? I am just getting started in Xcode so please explain why/how it works that way I can learn for the future. Thanks guys!
App Rendering (What I am trying to make)
http://imgur.com/wcNXu0z
Currently in Xcode (What I have so far)
http://imgur.com/PLUVaVg
You need to create an IBAction for add button. So its get called when you click the button and store the entered text in UItextfield in an array and you will use that array to add rows in your table.
For example consider the code :
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [dataArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = #"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
}
[cell.textLabel setText: [dataArray objectAtIndex:indexPath.row];
return cell;
}
-(IBAction)addCity:(id)sender
{
[tableView beginUpdates];
[dataArray addObject:#"City"];
NSArray *paths = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:[dataArray count]-1 inSection:1]];
[[self tableView] insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationTop];
[tableView endUpdates];
}
I'd strongly recommend you check out the free Sensible TableView framework. The framework should help you automatically create your text fields and automatically fill/save their data.

How can i use prototype cell in storyboard with search result controller

I have tableview with search result controller when search in search bar get this error indicate that there is no cell and get the below error .How can create my prototype cell in this method CellForRowAtIndexPath
Code :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"HoCell";
Ho *cell;
Ho *item;
if (tableView == self.searchDisplayController.searchResultsTableView) {
if (cell == nil)
{
cell = [[Ho alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"HoCell"];
}
item = [searchResultsController objectAtIndexPath:indexPath];
}
else{
cell = (Ho*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
item = [fetchedResultsController objectAtIndexPath:indexPath];
}
cell.ho.text = item.name;
cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"se.png"]];
return cell;
}
Error :
*** Assertion failure in -[UISearchResultsTableView _configureCellForDisplay:forIndexPath:], /SourceCache/UIKit_Sim/UIKit-2372/UITableView.m:5471
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
There may be two possibilities Here :
1) You are returning a number larger than your Array Count from
tableView:numberOfRowsInSection:. Don't.
2) One or more of your cell# outlets is not hooked up in your nib, or
is not hooked up to a UITableViewCell (or subclass). Hook them up
properly.
Go through this Ray Wenderlich's Link :
How to Add Search Into a Table View
Check this SO Questions :
1) UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath: Exception
2) ios 5 UISearchDisplayController crash
One More Beautiful Link : Custom Prototype Table Cells and Storyboards Just see this Portion :
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:UYLCountryCellIdentifier];
if (cell == nil)
{
[self.countryCellNib instantiateWithOwner:self options:nil];
cell = self.countryCell;
self.countryCell = nil;
}
// Code omitted to configure the cell...
return cell;
}
Your code seems to be buggy. You check for cell == nil while it is not set to nil initially. It also looks a bit strange that you allocate your cells in that way based on search mode.
Anyways, I would do it different way. Imho the way I do it is almost canonical :) Just use your search result to populate your cells with correct data for each case (search mode and normal mode). In this example, searchResult and dataSource are arrays with strings. I think in real life for you it will be something more complex like array of nsdictionary.
In your view controller:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)_section
{
/* Use correct data array so that in search mode we draw cells correctly. */
NSMutableArray *data = searching ? searchResult : dataSource;
return [data count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
/* Use correct data array so that in search mode we draw cells correctly. */
NSMutableArray *data = searching ? searchResult : dataSource;
static NSString *CellIdentifier = #"CellId";
CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[CustomTableViewCell alloc] initWithIdentifier:CellIdentifier] autorelease];
}
/* Note this is only correct for the case that you have one section */
NSString *text = [data objectAtIndex:[indexPath row]]
cell.textLabel.text = text;
/* More setup for the cell. */
return text;
}
And here are delegate methods for search controller and some helpers:
- (void) searchTableView
{
NSString *searchText = searchBar.text;
for (NSString *item in dataSource) {
NSRange range = [item rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (range.length > 0) {
[searchResult addObject:item];
}
}
}
- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller
{
searching = NO;
}
- (void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller
{
searching = NO;
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0]
withRowAnimation:UITableViewRowAnimationAutomatic];
[searchResult removeAllObjects];
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchString:(NSString *)searchText
{
[searchResult removeAllObjects];
if ([searchText length] > 0) {
searching = YES;
[self searchTableView];
} else {
searching = NO;
}
return YES;
}
Hope this helps.
I have the same issue, here is what is did and it is now working perfectly..
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Here is my code BEFORE
//
OPUsersTableViewCell *tableCell = [tableView dequeueReusableCellWithIdentifier:TableCellID];
// Here is my code AFTER
//
OPUsersTableViewCell *tableCell = [self.groupTableView dequeueReusableCellWithIdentifier:TableCellID];
Note:
self.groupTableView is where the prototype cell is...

uitableView reloadData doesn't work after setting delegate, datasource and file's owner connection

I have googled and done lot of research from my side to find out why the reloadData method on tableview wouldn't work. I checked all the possible solutions like the datasource is set, delegate is set, the tableview is connected to the file's owner.
After all these, when I am trying to reload the tableview, the no. of rows method gets called, but the cell for rowAtIndexPath doesn't get called. Below is the code that I have written. Please let me know, where I am going wrong
- (void)onReservationListSuccess:(NSArray *)rData
{
if ( rData != nil )
{
resList = [[NSArray alloc] initWithArray:rData];
if([resList count] > 0)
{
[self.tripsTableView reloadData];
//[self.tripsTableView beginUpdates];
//[self.tripsTableView reloadSections:[NSIndexSet indexSetWithIndex:0]
// withRowAnimation:UITableViewRowAnimationNone];
//[self.tripsTableView endUpdates];
}
else
{
[tripsTableView reloadData];
[tripsTableView setHidden:YES];
[noTripsLabel setHidden:NO];
}
}
if(fsnNeedsRefresh == YES)
{
[[NSNotificationCenter defaultCenter] postNotificationName:UpdateFSNList object:nil];
fsnNeedsRefresh = NO;
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
int temp=[resList count];
NSLog(#"The no. of rows are %d", temp);
NSLog(#"Testing Purpose");
NSLog(#"The pnr details of the object is:%#",((TripData *)[resList objectAtIndex:0]).pnrDescription);
return 1;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"The cell for the row at indexpath is getting called");
static NSString *CellIdentifier = #"TripCellIdentifier";
TripCell *cell = (TripCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"TripCell" owner:self options:nil];
for(id oneObject in nib)
if([oneObject isKindOfClass:[TripCell class]])
cell = (TripCell *)oneObject;
}
// Set up the cell...
TripData *tripData = (TripData *)[resList objectAtIndex:indexPath.row];
cell.pnrLabel.text = tripData.pnr;
NSLog(#"The cell text is %#",tripData.pnr);
cell.pnrDescriptionLabel.text = tripData.pnrDescription;
NSLog(#"The cell text is %#",tripData.pnrDescription);
cell.pnrTypeLabel.text = tripData.pnrType;
NSLog(#"The cell text is %#",tripData.pnrType);
if(checkInAllowed)
{
cell.checkInButton.tag = indexPath.row;
[cell.checkInButton addTarget:self action:#selector(checkIn:) forControlEvents:UIControlEventTouchUpInside];
}
else
{
[cell.checkInButton setEnabled:NO];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller
TripData *tripData = (TripData *)[resList objectAtIndex:indexPath.row];
NSLog(#"%#", tripData.pnr);
if(tripData != nil)
{
TripOverviewViewController *tripOverviewViewController = [[TripOverviewViewController alloc] initWithTrip:tripData];
[self.navigationController pushViewController:tripOverviewViewController animated:YES];
[tripOverviewViewController release];
}
[tableView deselectRowAtIndexPath:indexPath animated:NO];
}
From this part of code I cannot say exactly why it does not work but I'll try to explain how reloadData works.
First, how UITableView works: basically, it's a scrollview. When it is drawn, it checks how many rows it has, then checks their height and from its size and scroll position it decides which rows are currently displayed. Then it asks the delegate to return a UITableViewCell for every displayed row.
When the table is scrolled, it removes the hidden cells from the view hierarchy and adds the cells that have appeared.
And now the tricky part - what does reloadData do? It just removes all the UITableViewCells from the table hierarchy. Nothing more. The actual update is done when the table is drawn for the first time after reloadData.
So, my suggestion is - check that your table is not hidden and check its frame. Also, I see that you are accessing both a property getter self.tripsTableView and an ivar tripsTableView. This is confusing. Do they both return the same?

Searching using UISearchBar

I have a search bar added to the navigation bar. When the user types a word and clicks the search button (keyboard) I need to display results in the table related to the search.
When I click "Search", I get blank records (it actually goes inside the if condition in the searchBarSearchButtonClicked: method and adds the animal object too). I think there's a problem in
[mutableArray addObject:animal];
...
self.animalArray = mutableArray;
[self.tableView reloadData];
My complete code is as follows.
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
[self.mutableArray removeAllObjects];
for (Animal *animal in self.animalArray) {
if ([[animal nameOfAnimal] isEqualToString:[searchBar text]] ) {
[mutableArray addObject:animal];
}
}
self.animalArray = mutableArray;
[self.tableView reloadData];
[searchBar resignFirstResponder];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
Cell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
Animal *animal = [self.animalArray objectAtIndex:indexPath.row];
cell = [[Cell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
cell.nameofani.text=animal.nameOfAnimal;
cell.oriofani.text=animal.originOfAnimal;
}
return cell;
}
Make sure you implement tableView:numberOfRowsInSection: correctly, returning [self.animalArray count] for the search results table view.
try:
self.animalArray = [mutableArray copy];