UITableView Crashes when scrolling - iphone

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
NSLog(#"Returning num sections");
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(#"Returning num rows");
return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"Trying to return cell");
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] ;
}
cell.textLabel.text = #"Hello";
NSLog(#"Returning cell");
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"Selected row");
}
- (void)viewDidLoad
{
[super viewDidLoad];
m_titleTable = [[UITableView alloc] init] ;
m_titleTable.dataSource = self;
m_titleTable.delegate = self;
m_titleTable.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
[self.view addSubview:m_titleTable];
// Do any additional setup after loading the view from its nib.
}
-(void)titleAction:(id)sender{
NSLog(#"Calling");
UIViewController* popoverContent = [[UIViewController alloc]
init];
TitleViewController *popoverView = [[TitleViewController alloc]initWithNibName:#"TitleViewController" bundle:nil];
popoverContent.view = popoverView.view;
//resize the popover view shown
//in the current view to the view's size
popoverContent.contentSizeForViewInPopover = CGSizeMake(150, 150);
//create a popover controller
self->popoverController = [[UIPopoverController alloc]
initWithContentViewController:popoverContent];
//present the popover view non-modal with a
//refrence to the button pressed within the current view
[self->popoverController presentPopoverFromRect:btn_title.frame
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionUp
animated:YES];
}
The tableview Crash error message
[__NSCFType tableView:cellForRowAtIndexPath:]: unrecognized selector sent to instance 0x6e7dca0
2012-08-28 14:17:10.539 Demo[2790:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFType tableView:cellForRowAtIndexPath:]: unrecognized selector sent to instance 0x6e7dca0'

Your problem is that your view controller is being deallocated. You shouldn't be doing something like this:
UIViewController* popoverContent = [[UIViewController alloc]
init];
TitleViewController *popoverView = [[TitleViewController alloc]initWithNibName:#"TitleViewController" bundle:nil];
popoverContent.view = popoverView.view;
Assuming you are using ARC, your popoverView object will be deallocated. Also this is just incorrect. You should almost never instantiate a UIViewController, and definitely never assign one view controller's view instance to another ones!
Here's how I'd rewrite your titleAction: method:
TitleViewController *titleViewController = [[TitleViewController alloc]initWithNibName:#"TitleViewController" bundle:nil];
//resize the popover view shown
//in the current view to the view's size
popoverContent.contentSizeForViewInPopover = CGSizeMake(150, 150);
//create a popover controller
self.popoverController = [[UIPopoverController alloc]
initWithContentViewController:titleViewController];
//present the popover view non-modal with a
//refrence to the button pressed within the current view
[self.popoverController presentPopoverFromRect:btn_title.frame
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionUp
animated:YES];
The UIPopoverController, at least, should keep a strong reference to the titleViewController and prevent it from being deallocated.
PS I changed your "self->popoverController" to self.popoverController as the -> is not really correct for what you are trying to do. Dot notation makes explicit that you are setting a property on an object.

in viewDidLoad you allocates and initiates a UITableView and connect it to self. You don't have a UITableView? defined and connect in the nib? Without seeing your whole project my guess is that you have UITableViews - this is a problem but not necessarily the crash problem. Did you inherit from UITableViewController as base class?

Related

Error on opening popviewcontroller in iOS 7

It looks a simple thing to me but I am not able to understand what mistake I am making. I have to open a popover on click of row in my iPad application. I have done following code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
popViewController *vc = [[popViewController alloc] initWithNibName:#"popViewController" bundle:nil];
vc.preferredContentSize = CGSizeMake(500,500);
vc.view.frame = CGRectMake(0, 0, 500, 500);
UIPopoverController *healthPopOver = [[UIPopoverController alloc] initWithContentViewController:vc];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[healthPopOver presentPopoverFromRect:cell.bounds inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
The app crashes when last line is executed. I have searched lots of pages on the web, but I am not able to find its cause. I am not getting any specific error only the Thread 1 error in the main file.
I am using iOS 7.
Try to add your healthPopOverit a member of your class since UIPopoverControllers must be held in an instance variable.
In your .m define it as a property:
UIPopoverController *healthPopOver;
and change:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
popViewController *vc = [[popViewController alloc] initWithNibName:#"popViewController" bundle:nil];
vc.preferredContentSize = CGSizeMake(500,500);
vc.view.frame = CGRectMake(0, 0, 500, 500);
self.healthPopOver = [[UIPopoverController alloc] initWithContentViewController:vc];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[healthPopOver presentPopoverFromRect:cell.bounds inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}

iOS app freezes after UITableView search

I am a beginner to iOS development and am having trouble finding the cause for this issue.
I have a simple app with a Master and DetailView controllers. The Master View contains an UITableView with a SearchController. Selecting any row on the UITableView will transition to the detailview.
The app freezes when I perform the following sequence
Launch app
Pull down the search bar
Enter text
Select a row from the search results
Select back from the detail view
Now the app freezes after the ViewDidLoad method of the MasterView is loaded. I can't find anything in the system.log.
Here's the code from the MasterViewController
- (void)viewWillAppear:(BOOL)animated {
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 44.0f)];
[self.tableView setContentOffset:CGPointMake(0,40)];
self.tableView.tableHeaderView = self.searchBar;
// Create and configure the search controller
self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
self.searchController.searchResultsDataSource = self;
self.searchController.searchResultsDelegate = self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *months = #"Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec";
feeds = [months componentsSeparatedByString:#","];
self.navigationItem.leftBarButtonItem = self.editButtonItem;
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(insertNewObject:)];
self.navigationItem.rightBarButtonItem = addButton;
self.detailViewController = (DetailViewController *)[[self.splitViewController.viewControllers lastObject] topViewController];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(#"numberOfRowsInSection");
if(tableView == self.tableView)
{
return feeds.count;
}
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"SELF like %#", self.searchBar.text];
self.filteredFeeds = [feeds filteredArrayUsingPredicate:predicate];
return feeds.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
}
cell.textLabel.text = [feeds objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
NSDate *object = _objects[indexPath.row];
self.detailViewController.detailItem = object;
}
[self performSegueWithIdentifier: #"showDetail" sender: self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"showDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSDate *object = _objects[indexPath.row];
[[segue destinationViewController] setDetailItem:object];
}
}
I have uploaded the entire project at the location below.
https://www.dropbox.com/s/yok9vngzv143npa/search.zip
Any kind of assistance is appreciated.
I can not able to run your program because version issue of iOS.
But i seen your code. I suspect on 1 point on following code,
- (void)viewWillAppear:(BOOL)animated {
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 44.0f)];
[self.tableView setContentOffset:CGPointMake(0,40)];
self.tableView.tableHeaderView = self.searchBar;
// Create and configure the search controller
self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
self.searchController.searchResultsDataSource = self;
self.searchController.searchResultsDelegate = self;
}
You create/allocate UISearchBar and UISearchDisplayController again and again while you get back from DetailViewController to MasterViewController, these may be cause of your app freezing.
"viewWillAppear" called before the receiver’s view is about to be added to a view hierarchy and before any animations are configured for showing the view.
"viewDidLoad" called after view controller has loaded its view hierarchy into memory.
I suggest you place your one time allocations view code in viewDidLoad method.
For more information go through this doc https://developer.apple.com/library/ios/documentation/uikit/reference/UIViewController_Class/Reference/Reference.html#//apple_ref/occ/instm/UIViewController/viewDidLoad

How to link a UITableViewController to multiple ViewControllers and how to call them

I'm trying to link cells from an UITableViewController to UIViewController, but each cell has to call a different UIViewController (so I imagine that I'll have to create one link to each view controller) and I don't know how to link them through storyboard.
This is what I have until now:
MainView.h
#interface MainView : UITableViewController <UITableViewDelegate, UITableViewDataSource>
{
NSArray *tableData;
}
#property (nonatomic, retain) NSArray *tableData;
#end
MainView.m
- (void)viewDidLoad
{
tableData = [[NSArray alloc] initWithObjects:#"1", #"2", #"3", #"4", nil];
[super viewDidLoad];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [tableData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:#"MyCell"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"MyCell"];
}
cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
UIViewController *anotherVC = nil;
if (indexPath.section == 0) //here I've also tried to use switch, case 0, etc
{
if (indexPath.row == 0)
{
anotherVC = [[ViewForFirstRow alloc] init];
}
NSLog(#"anotherVC %#", anotherVC); // shows that anotherVC = ViewForFirstRow
[anotherVC setModalPresentationStyle:UIModalPresentationFormSheet];
[anotherVC setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[self.navigationController pushViewController:anotherVC animated:YES];
}
}
#end
So, when first row is selected and I didn't link UITableViewController to UIViewController (ViewForFirstRow), it shows a black screen.
If I link UITableViewController to ViewForFirstRow, it opens the view but without the effect I used in code [self.navigationController pushViewController:anotherVC animated:YES];, so it appears that it called the view due to the link and not because of the code [self.navigationController pushViewController:anotherVC animated:YES];
How can I call the view by code properly and what kind of link should I use in storyboard for that? Also, how can I link the UITableViewController to multiples UIViewController (ViewForFirstRow, ViewForSecondRow, ViewForThirdRow, etc).
Thanks!
First, you can't hook up segues from cell (in a dynamic table view) in the storyboard, you have to do it in code. The code should look like this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if (indexPath.row == 0) {
UIViewController *anotherVC = [self.storyboard instantiateViewControllerWithIdentifier:#"anotherVC"];
[self.navigationController pushViewController:anotherVC animated:YES];
}else if (indexPath.row == 1) {
UIViewController *someOtherVC = [self.storyboard instantiateViewControllerWithIdentifier:#"someOtherVC"];
[self.navigationController pushViewController:someOtherVC animated:YES];
}
}
You shouldn't be doing anything with indexPath.section since your table view only has one section.
In storyboard presenting another viewcontroller in modally manner then use this:
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
Synergy *objSynergy = (Synergy *)[mainStoryboard instantiateViewControllerWithIdentifier:#"Synergy"];
[self presentModalViewController:objSynergy animated:YES];
Synergy is my controller name You can put here your controller name that you want to present.
Make changes with your requirement. And for different viewcontroller open from different row
Use this:
if (indexPath.section==0)
{
if (indexPath.row ==0)
{
}
}

Activity Indicators when pushing next view - didSelectRowAtIndexPath

I can successfully push only next view in my iPhone app. However, cause the next view retrieves data to populate the UITableViews, sometimes waiting time could be a few seconds or slightly longer depending on connection.
During this time, the user may think the app has frozen etc. So, to counter this problem, I think implementing UIActivityIndicators would be a good way to let the user know that the app is working.
Can someone tell me where I could implement this?
Thanks.
pushDetailView Method
- (void)pushDetailView {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
//load the clicked cell.
DetailsImageCell *cell = (DetailsImageCell *)[tableView cellForRowAtIndexPath:indexPath];
//init the controller.
AlertsDetailsView *controller = nil;
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
controller = [[AlertsDetailsView alloc] initWithNibName:#"DetailsView_iPad" bundle:nil];
} else {
controller = [[AlertsDetailsView alloc] initWithNibName:#"DetailsView" bundle:nil];
}
//set the ID and call JSON in the controller.
[controller setID:[cell getID]];
//show the view.
[self.navigationController pushViewController:controller animated:YES];
You can implement this in didSelectRowAtIndexPath: method itself.
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleWhiteLarge];
cell.accessoryView = spinner;
[spinner startAnimating];
[spinner release];
This will show the activity indicator at the right side of the cell which will make the user feel that something is loading.
Edit: If you set the accessoryView and write the loading code in the same method, the UI will get updated only when all the operations over. A workaround for this is to just set the activityIndicator in the didSelectRowAtIndexPath: method and call the view controller pushing code with some delay.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleWhiteLarge];
cell.accessoryView = spinner;
[spinner startAnimating];
[spinner release];
[self performSelector:#selector(pushDetailView:) withObject:tableView afterDelay:0.1];
}
- (void)pushDetailView:(UITableView *)tableView {
// Push the detail view here
}

how display view when cells select in a tableview

i am new to iphone ,my question is in one view i placed a tableview and display some data on that view,i want to add another view(same view to all cell) when select cell(suppose select first cell view will display and newly add view is used to delet that cell information based on cell index)please tell me how to display view.
In the didSelectRowAtIndexPath method in your TableView class; that is where you deal with the selected row. Here is some code that pushes a webViewController onto the screen :
webViewController = [[WebViewController alloc] init];
WebViewController *wvc = [[WebViewController alloc] init];
NSMutableArray *temp = [eventsDict objectForKey:[datesArray objectAtIndex:[indexPath section]]];
EventObject *tempEvent = [temp objectAtIndex:[indexPath row]];
NSString *eventURL = [tempEvent eventLink];
[wvc setEventWebsite:eventURL];
[self setWebViewController:wvc];
[[self navigationController] pushViewController:webViewController animated:YES];
[wvc release];
[webViewController release];
Basically I have an array of Events, it gets the event you touched using [indexPath row], gets the URL within that Event object, passes it to the webViewController, then it pushes the webViewController onto the screen loading the appropriate link. Hope this helps
If you want to display the same view for all cells - simply create one UIView object and change frame in didSelectRowAtIndexPath delegate method. It will something like this -
At first declare infoView in your .h file UIView *infoView, next steps:
- (void)viewDidLoad {
infoView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
infoView.hidden = YES;
[tableView addSubview:infoView];
[super viewDidLoad]; }
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
CGPoint st = CGPointMake(tableView.frame.size.width/2, cell.frame.size.height*indexPath.row);
infoView.hidden = NO;
infoView.center = st;}
Don't forget to release infoView in dealloc method