how can I make clickable url in NSTableView - objective-c

I am trying to build OS-X core data based app. In one of the entities, I am storing an URL ex. (www.somesite.com/somepage/someindex.php)
Using binding, I am successfully displaying the URL in the NSTableView. I would like however that URL to be clickable, and when clicked, browser to fire up and open the page. I have done some research, and I have found some solutions, for example:
Clickable url link in NSTextFieldCell inside NSTableView?
also:
https://developer.apple.com/library/mac/qa/qa1487/_index.html
but they both look outdated, first one is six years old, while the second is last updated on Jan. 2005
Anyone can provide easier & faster way how to achieve this? I didn't expected that I will have to write bunch of code just to make simple link to work to be honest... I am coming from web development world, where those kind of things can be sorted out withing few seconds, while here seems to be totally different story....
Any help will be appreciated.
John

You can use NSTextView and implement its delegate. There is a demo:
// MyCellView.h
#interface MyCellView : NSView
#property (nonatomic, strong) IBOutlet NSTextView *textView;
#end
// ViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.delegate = self;
self.tableView.dataSource = self;
NSNib *nib = [[NSNib alloc] initWithNibNamed:#"MyCellView" bundle:[NSBundle mainBundle]];
[self.tableView registerNib:nib forIdentifier:#"MyCell"];
}
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
MyCellView *cell = (MyCellView *)[tableView makeViewWithIdentifier:#"MyCell" owner:self];
cell.textView.delegate = self;
[cell.textView.textStorage setAttributedString:[self makeLinkAttributedString:#"This is a test: www.somesite.com/somepage/someindex.php"]];
return cell;
}
- (NSAttributedString *)makeLinkAttributedString:(NSString *)string {
NSMutableAttributedString *linkedString = [[NSMutableAttributedString alloc] initWithString:string];
NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:nil];
[detector enumerateMatchesInString:string options:0 range:NSMakeRange(0, string.length) usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop) {
if (match.URL) {
NSDictionary *attributes = #{ NSLinkAttributeName: match.URL };
[linkedString addAttributes:attributes range:match.range];
}
}];
return [linkedString copy];
}
#pragma mark - NSTextViewDelegate methods
- (BOOL)textView:(NSTextView *)textView clickedOnLink:(id)link atIndex:(NSUInteger)charIndex {
// The click will be handled by you or the next responder.
return NO;
}

You can use TTTAttributedLabel in your tableviewcell. It supports powerful link detection.

Related

Hiding an autocomplete UITableView that was created programmatically

I'm building an application with an autocomplete UITableView from this tutorial. I have the autocomplete functionality working properly, but I would like the UITableView-autocomplete drop down to disappear when the word is clicked on or when it is touched up outside. I'm not sure how to set up a delegate when the object is set up programmatically. I've only done this using the interface builder.
.h
#interface slrpViewController : UIViewController<UITextFieldDelegate, UIPickerViewDelegate, UIPickerViewDataSource>
{
NSMutableArray *dataArray;
NSMutableData *receivedData;
NSMutableArray *pastUrls;
NSMutableArray *autocompleteUrls;
UITableView *autocompleteTableView;
}
#property(nonatomic, retain) IBOutlet UITextField *eWordEntered;
#property (nonatomic, retain) NSMutableArray *pastUrls;
#property (nonatomic, retain) NSMutableArray *autocompleteUrls;
#property (retain, nonatomic) NSMutableData *responseData;
#property (nonatomic, retain) UITableView *autocompleteTableView;
-(void)setReceivedData:(NSMutableData*)pReceivedData;
-(NSMutableData *) getReceivedData;
-(void) getAutoCompleteArray;
-(void)searchAutocompleteEntriesWithSubstring:(NSString *)substring;
.m
- (void)viewDidLoad
{
[super viewDidLoad];
[self getAutoCompleteArray];
pastUrls = [[NSMutableArray alloc] init];
NSLog(#"In the viewDidLoad and pasturl is: %#", self.pastUrls);
self.autocompleteUrls = [[NSMutableArray alloc] init];
autocompleteTableView = [[UITableView alloc] initWithFrame:CGRectMake(210, 225, 310, 120) style:UITableViewStylePlain];
self.autocompleteTableView.delegate = self;
self.autocompleteTableView.dataSource = self;
autocompleteTableView.scrollEnabled = YES;
autocompleteTableView.hidden = YES;
[self.view addSubview:autocompleteTableView];
-(void)setReceivedData:(NSMutableData*)pReceivedData
{
receivedData = pReceivedData;
}
-(NSMutableData *) getReceivedData{
return receivedData;
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[receivedData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{[receivedData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSError *e = nil;
NSError *error = nil;
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData: receivedData options: NSJSONReadingMutableContainers error: &e];
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:receivedData
options:kNilOptions
error:&error];
seneca_word.ids = [jsonDict objectForKey:#"ids"];
NSArray *array_ids = [jsonDict objectForKey:#"ids"];
NSString *ids = array_ids[0];
seneca_word.ids = ids;
for (id key in jsonDict)
{
NSLog(#"key: %#, value: %#", key, [jsonDict objectForKey:key]);
NSLog(#"The value of bases by itself is: %#", [jsonDict objectForKey:#"bases"]);
}
if (!jsonArray)
{
NSLog(#"Error parsing JSON: %#", e);
}
else
{
if([jsonDict objectForKey:#"english"] != nil){
pastUrls = [jsonDict objectForKey:#"bases"];
}
else{
//Some of JSON object that I don't want to use here
}//else
}//(void)connectionDidFinishLoading
- (void)searchAutocompleteEntriesWithSubstring:(NSString *)substring {
[autocompleteUrls removeAllObjects];
for(NSString *curString in pastUrls) {
NSRange substringRange = [curString rangeOfString:substring];
if (substringRange.location == 0) {
[autocompleteUrls addObject:curString];
}
}
[autocompleteTableView reloadData];
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
autocompleteTableView.hidden = NO;
NSString *substring = [NSString stringWithString:textField.text];
substring = [substring stringByReplacingCharactersInRange:range withString:string];
[self searchAutocompleteEntriesWithSubstring:substring];
return YES;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger) section {
return autocompleteUrls.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = nil;
static NSString *AutoCompleteRowIdentifier = #"AutoCompleteRowIdentifier";
cell = [tableView dequeueReusableCellWithIdentifier:AutoCompleteRowIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:AutoCompleteRowIdentifier];
}
cell.textLabel.text = [autocompleteUrls objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
self.eWordEntered.text = selectedCell.textLabel.text;
if(tableView == autocompleteTableView){
//The autocomplete table view is the one that fired the didSelect delegate method
//So hide the autocomplete table.
//do whatever else you need to do to empty the autocompleteTableView's data source
//or/and simply hide the table after that
[autocompleteTableView setHidden:YES];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//When the user clicks outside of the uitableview it will disappear
[autocompleteTableView setHidden:YES];
}
As you can see I populate the autocomplete UITableView with JSON data that I'm getting from a RESTful API.
I'm getting the warning, Assigning to 'id<UITableViewDelegate>' from incompatible type 'ViewController *const __strong' for the lines:
self.autocompleteTableView.delegate = self;
self.autocompleteTableView.dataSource = self;
I imagine once I get the delegate stuff sorted out I'll be able to do what I want. I did some research and tried to create a delegate class but wasn't able to get that solution working. I'm not even sure if that's the right way to go about this as I usually do this stuff by interface builder and not programmatically. Any direction or help is greatly appreciated. Thanks!
You should be using the tableView's didSelectCellAtIndexPathRow delegate method to identify user taps on a cell from a tableView. It's ok if you created your tableView progammatically.
Simply make sure the UIViewController conforms to the UITableViewDelegate and UITableViewDataSource` protocols.
make sure you set the tableView's delegate and dataSource property to self.
Implement the didSelectCellAtIndexPathRow delegate method in your viewController's .m file like so:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}
Then inside that delegate method you need to help detect from which tableView your didSelect method got fired from as you only want to hide the autocomplete table when the user selects a cell from that table. So you do a simple tableView check like so:
if(tableView == autocompleteTableView){
//The autocomplete table view is the one that fired the didSelect delegate method
//So hide the autocomplete table.
//do whatever else you need to do to empty the autocompleteTableView's data source
//or/and simply hide the table after that
[autocompleteTableView setHidden:YES];
}
You probably also want to make sure that you set the autocompleteTableView hidden property to NO when the user types in something in the textfield so that the auto complete can show appear again.
And thats all buddy.
try setting self.autocompleteTableView.hidden = YES;

Mocking UITableViewCell and using isKindOfClass

I'm trying to mock and test UITableViewCells to make sure my configureCell:forIndexPath works correctly, except I can't get it to work using isKindOfClass but only conformsToProtocol. This would require all of my uitableviewcells to have it's own protocol and does not seem needed.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
FeedObj *item = [_feedElements objectAtIndex:indexPath.row];
if( item.obj_type == FeedObjTypeFriendAdd ) {
MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyTableViewCellIdentifier forIndexPath:indexPath];
[self configureCell:cell forIndexPath:indexPath]
return cell;
} else if( item.obj_type = FeedObjTypeSomeOtherType ) {
// do another cell
}
}
- (void)configureCell:(UITableViewCell *)cell forIndexPath:(NSIndexPath *)indexPath
{
// only enters conditional in test if I do [cell conformsToProtocol:#protocol(SomeIndividualProtocolForEachTableViewcell)]
if( [cell isKindOfClass:[MyTableViewCell class]] ) {
// do the configuring
FeedObj *item = [_streamElements objectAtIndex:indexPath.row];
NSString *firstName = [item.obj_data objectForKey:#"first_name"];
NSString *lastName = [item.obj_data objectForKey:#"last_name"];
NSString *name = [NSString stringWithFormat:#"%# %#.", firstName, [lastName substringToIndex:1]];
NSString *text = [NSString stringWithFormat:#"%# has joined", name];
[((MyTableViewCell *)cell).messageLabel setText:text];
} else if( [cell isKindOfClass[SomeOtherTableView class]] ) {
// do other config
}
}
#implementation SampleTests
- (void)setUp
{
_controller = [[MySampleViewController alloc] init];
_tableViewMock = [OCMockObject niceMockForClass:[UITableView class]];
[_tableViewMock registerNib:[UINib nibWithNibName:#"MyTableViewCell" bundle:nil] forCellReuseIdentifier:MyTableViewCellIdentifier];
}
- (void)testFriendAddCell
{
FeedObj *friendAdd = [[FeedObj alloc] init];
friendAdd.obj_type = FeedObjTypeFriendAdd;
friendAdd.obj_data = [NSMutableDictionary dictionaryWithDictionary:#{ #"first_name" : #"firstname", #"last_name" : #"lastname" }];
_mockStreamElements = [NSMutableArray arrayWithObject:friendAdd];
[_controller setValue:_mockStreamElements forKey:#"_feedElements"];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[[[_tableViewMock expect] andReturn:[[[NSBundle mainBundle] loadNibNamed:#"MyTableViewCell" owner:self options:nil] lastObject]] dequeueReusableCellWithIdentifier:MyTableViewCellIdentifier forIndexPath:indexPath];
MyTableViewCell *cell = (MyTableViewCell *)[_controller tableView:_tableViewMock cellForRowAtIndexPath:indexPath];
STAssertNotNil( cell, #"should not be nil" );
STAssertTrue( [cell.messageLabel.text isEqualToString:#"firstname l. has joined"], #"should be equal" );
[_tableViewMock verify];
}
#end
I've also tried doing [[[mockCell stub] andReturnValue:OCMOCK_VALUE((BOOL) {YES})] isKindOfClass:[MyTableViewCell class]]] with a mockCell expect and it doesn't work either. Like this:
id mockCell = [OCMockObject partialMockForObject:[[[NSBundle mainBundle] loadNibNamed:#"MyTableViewCell" owner:self options:nil] lastObject]];
[[[mockCell stub] andReturnValue:OCMOCK_VALUE((BOOL) {YES})] isKindOfClass:[OCMConstraint isKindOfClass:[MyTableViewCell class]]];
[[[_tableViewMock expect] andReturn:mockCell] dequeueReusableCellWithIdentifier:MyTableViewCellIdentifier forIndexPath:indexPath];
I even tried with an OCMConstraint listed in http://blog.carbonfive.com/2009/02/17/custom-constraints-for-ocmock/.
Is there anyway to do this or do I have to use protocols for each tableviewcell? Thanks in advance
I'd strongly suggest you rethink how you are building out this implementation. For starters, a view controllers is great at managing a view, but managing your model data is not what it's for. Its good for passing around your model data to the views it manages, so with that in mind, let's build this out like that.
Let's start by introducing a new class, called FeedController. This controller's job is to sit between your VC and your model data backing this screen. Let's assume this public interface:
#interface FeedController : NSObject
- (instancetype)initWithFeedArray:(NSArray *)array;
- (NSString *)firstNameAtIndexPath:(NSIndexPath *)path;
- (NSString *)lastNameAtIndexPath:(NSIndexPath *)path;
- (NSString *)fullNameAtIndexPath:(NSIndexPath *)path;
// This should probably have a better name
- (NSString *)textAtIndexPath:(NSIndexPath *)path;
#end
I'm not going to implement these methods, but they'd look exactly like you'd expect. The initializer would copy the array passed into it, store it in an ivar, and the other methods would take the piece of info out of the array at the specific index and apply any custom transformations you must (like combining the first and last name to get the full name). The main goal here is to transfer the data, not manipulate it. The moment you try and manipulate this data in your view controller, is the moment you'll be back to square one, testing wise.
The object of your configureCell:forIndexPath: is now just to transfer data from the FeedController class, which is infinitely simple to test. No need to set up a responder chain, mock out objects, or anything. Just supply some fixture data and away you go.
You are still testing the pieces that make up your configureCell:forIndexPath: but not directly testing that method anymore. If you want to make sure that the view is being populated correctly, great, you should. However, you'll do this differently, this isn't a job for unit tests. Pull out UIAutomation or your favourite UI testing framework, and test your UI. Use the unit tests on the FeedController to test your data and transformations.
I hope this helps.

UIImage Causing Views All Throughout the iPhone to Turn Black

Alright, this problem is rather bizarre, and I'm 99% positive at this point it's not a retain/release error. Basically I'm using UIImagePickerController to grab an image from either the library or the camera, but the resulting image causes some... strange things to happen. Though it's laggy to pull the image up in a detail view controller containing a UIImageView, the real problem is that if I repetitively pull up the view controller and dismiss it, eventually parts of the image will have disappeared!!! More disturbingly, if I attempt to pull it up again after this has happened, the whole image will have turned black, and other UIViews throughout both my App AND the iPhone itself will either flicker wildly or have turned black!!!! For example, both the iPhone wallpaper and the initial "slide to unlock" panel turn black and flicker, respectively... It makes no difference whether the photo came from the library or the camera. The problem is entirely averted (no lag, no blackness) if I do something like this:
//UIImage* image = (UIImage*)[info objectForKey:UIImagePickerControllerOriginalImage];
UIImage* image = [[UIImage alloc] initWithData:
[NSData dataWithContentsOfURL:
[NSURL URLWithString:
#"http://wild-facts.com/wp-content/uploads/2010/07/Diving_emperor_penguin.jpg"]]];
Thus I can't imagine the problem having to do with anything other than the UIImagePickerController. Note that I'm still creating and dismissing it-- I'm just not grabbing the image out of it.
At first I thought the image was simply being overreleased, and the disappearing black chunks were parts of memory being overwritten. However, changing the image's retain count makes no difference. Then I thought, maybe the image is too large and somehow not getting properly released when I dismiss the UIImageView! Yet downloading a several MB image from the internet will not replicate the error. Then I thought, maybe the UIImagePickerController is disposing of the image, regardless of retain count! But copying the image failed as well. Furthermore, how could any of these things effect views that exist as deep as the iOS level?! I've researched, experimented, and Googled and no one has encountered this problem except for me... yet I'm not doing particularly strange! Is this an Apple issue? Did I forget something obvious? I've scanned up and down the documentation and guides to no avail, but perhaps I missed something.
None of this has worked:
Incrementing the retain count
Using [image copy]
None of this has replicated the problem with the downloaded image:
Decrementing the retain count
Downloading an image of size greater than 1 MB with large dimensions
I'm using the latest Verizon iPhone with iOS 4.2.8 (base SDK "overriden" to 4.3, whatever that means). 4.2.8 is the latest possible one for Verizon, though 4.3 is available for iPhones using AT&T.
Here's the code in glorious detail. I'm not yet checking for device compatibility, but it shouldn't matter concerning this. Perhaps I forgot some setting with the UIImagePickerController?
Quick Overview: I display an action sheet, then based on the user's input, display the image picker. I save the image as a transformable attribute on a Core Data object (delivery) using a custom transformer. I later hand the image to a detail view controller to display to the user.
IGDeliveryVC.m (parts of it, anyways. It's a tableview displaying the delivery's added media)
- (void)refresh
{
[mediaDisplayArray release];
NSSortDescriptor* sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:#"displayIndex" ascending:YES];
mediaDisplayArray = [[NSMutableArray alloc] initWithArray:
[delivery.deliveryMedia sortedArrayUsingDescriptors:
[NSArray arrayWithObject:sortDescriptor]]];
if (mediaDisplayArray == nil)
mediaDisplayArray = [[NSMutableArray alloc] init];
[self.tableView reloadData];
}
- (void)onAddMedia:(id)sender
{
#if TARGET_IPHONE_SIMULATOR
UIImage* image = [[UIImage alloc] initWithData:
[NSData dataWithContentsOfURL:
[NSURL URLWithString:
#"http://wild-facts.com/wp-content/uploads/2010/07/Diving_emperor_penguin.jpg"]]];
[delivery addImage:image];
[self refresh];
[image release];
return;
#endif
UIActionSheet *options = [[UIActionSheet alloc] initWithTitle:#"Add Media from..."
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:#"Camera", #"Library", nil];
[options showFromToolbar:self.navigationController.toolbar];
[options release];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex != 0 && buttonIndex != 1)
return;
UIImagePickerController* picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = NO;
picker.mediaTypes = [NSArray arrayWithObjects:#"public.image",#"public.movie", nil];
switch (buttonIndex)
{
case 0:
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
break;
case 1:
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
break;
}
[self presentModalViewController:picker animated:YES];
}
- (void) imagePickerControllerDidCancel: (UIImagePickerController *) picker
{
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
[picker release];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString* mediaType = (NSString*)[info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:#"public.image"])
{
UIImage* image = (UIImage*)[info objectForKey:UIImagePickerControllerOriginalImage];
//UIImage* image = [[UIImage alloc] initWithData:
// [NSData dataWithContentsOfURL:
// [NSURL URLWithString:
// #"http://wild-facts.com/wp-content/uploads/2010/07/Diving_emperor_penguin.jpg"]]];
[delivery addImage:image];
}
else if ([mediaType isEqualToString:#"public.movie"])
{
NSString* videoURL = (NSString*)[info objectForKey:UIImagePickerControllerMediaURL];
[delivery addVideo:videoURL];
}
else
{
NSLog(#"Error: imagePickerController returned with unexpected type %#", mediaType);
}
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
[picker release];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
IGMedia* media = [mediaDisplayArray objectAtIndex:indexPath.row];
UIViewController* detailViewController =
[media isMemberOfClass:[IGMediaImage class]]
? [[IGMediaImageDetailVC alloc] initWithImage:((IGMediaImage*)media).image]
: nil;
if (detailViewController != nil)
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
}
IGMediaImageDetailVC.h (I use a xib :P )
#import <UIKit/UIKit.h>
#interface IGMediaImageDetailVC : UIViewController {
}
#property (nonatomic, retain) UIImage* image;
#property (nonatomic, retain) IBOutlet UIImageView* imageView;
- (id)initWithImage:(UIImage*)anImage;
#end
IGMediaImageDetailVC.m
#import "IGMediaImageDetailVC.h"
#implementation IGMediaImageDetailVC
#synthesize image;
#synthesize imageView;
- (id)initWithImage:(UIImage*)anImage
{
self = [super initWithNibName:#"IGMediaImageDetailVC" bundle:nil];
if (self)
{
self.image = anImage;
}
return self;
}
- (void)dealloc
{
[image release];
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
self.imageView.image = self.image;
}
- (void)viewDidUnload
{
[super viewDidUnload];
[imageView release];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
If there's anything I can do to make this post more legible, please let me know. I'll add things that don't work/replicate the problem to the appropriate list. Thanks for taking the time to read this!
I just figured out the problem, had to do with excessive memory use. Thanks goes to this amazing post: https://stackoverflow.com/questions/1282830/uiimagepickercontroller-uiimage-memory-and-more Basically, before ever displaying the image, I divide each dimension by 4, resulting in a total 1/16th of the memory I was using before. Here's the method I used (as per the awesome post):
+ (UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize;
{
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
In the method call, I pass in CGSizeMake(image.size.width/4, image.size.height/4)
Hope this was helpful!

Why is my tab bar controller crashing?

I'm trying to create a iPhone app that uses a tab bar controller. The first tab works fine.
However, when I click the second tab in the tab bar the whole app crashes. I am trying to implement a table view in the second tab.What could be causing the crash?
Here is my code:
SecondViewController.h
#import <UIKit/UIKit.h>
#class Person;
#interface SecondViewController : UIViewController<UITableViewDelegate, UITableViewDataSource >{
UITableView *tableView;
NSArray *persons;
}
#property (nonatomic, retain) IBOutlet UITableView *tableView;
#property (nonatomic,retain ) NSArray *persons;
-(void)initPersons:(NSArray *) array;
#end
SecondViewController.m
#import "SecondViewController.h"
#import "Person.h"
#implementation SecondViewController
#synthesize tableView;
#synthesize persons;
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization.
}
return self;
}
*/
- (id)init {
if (self = [super initWithNibName:#"SecondViewController" bundle:nil]) {
//self.title = #"Slot";
UIImage* anImage = [UIImage imageNamed:#"cherry.png"];
UITabBarItem* theItem = [[UITabBarItem alloc] initWithTitle:#"table" image:anImage tag:0];
self.tabBarItem = theItem;
[theItem release];
}
return self;
}
-(void)initPersons:(NSArray *) array{
int size = [array count];
int i = 0;
NSMutableArray *aPersons = [NSMutableArray array];
while (i < size) {
Person *person = [[Person alloc]init];
NSString * name =[array objectAtIndex:i];
NSArray *chunks =[name componentsSeparatedByString: #" "];
person.firstName = [chunks objectAtIndex:0];
person.lastName = [chunks objectAtIndex:[chunks count]-1];
[aPersons addObject:person];
[person release];
i++;
}
self.persons=aPersons;
[aPersons release];
}
-(NSArray *)sortArray {
NSSortDescriptor *lastNameDescriptor = [[[NSSortDescriptor alloc]
initWithKey:#"lastName"
ascending:YES
selector:#selector(localizedCaseInsensitiveCompare:)] autorelease];
NSSortDescriptor *firstNameDescriptor = [[[NSSortDescriptor alloc]
initWithKey:#"firstName"
ascending:YES
selector:#selector(localizedCaseInsensitiveCompare:)] autorelease];
NSArray *sortDescriptors = [NSArray arrayWithObjects:lastNameDescriptor,
firstNameDescriptor, nil];
return [persons sortedArrayUsingDescriptors:sortDescriptors];
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
NSArray *array = [[NSArray alloc] initWithObjects:
#"Amin Alrusayni", #"Berksan Ates",
#"Becca Bedell", #"Joseph Carioti",
#"Christopher Conry", #"Jeremy Dobbins", #"Timothy Fox",
#"Eric Green", #"Timothy Gruscinski", #"Daniel Gur",
#"Yousef Guzaiz", #"Tyler Herzog", #"Alicia Johnson", #"Scott Kazakis",
#"Nathan Kidwell", #"Dakota Kincer", #"Scott Moore",
#"Sean Reber", #"Michael Romeo", #"Alexander Shibble",
#"Joshua Shipley", #"Mitchell Slemc", #"Thomas Smith",
#"Christopher Wagner", #"Corey Zachrich", #"Ahmed Alalawi",
#"Abdullah Alqahtani", #"Daniel Angelis", #"Brian Bartman",
#"William Haverstock", #"Hui Hong", #"Rong Li",
#"Amitkumar Mali", #"Christian Newman", nil];
[self initPersons:array];
NSArray *sortedArray = [self sortArray];
for (Person *person in sortedArray)
{
NSString *fullName = [[person.firstName stringByAppendingString:#" "] stringByAppendingString:person.lastName];
NSLog(#"%#",fullName);
NSLog(#" ");
}
[super viewDidLoad];
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations.
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
//commented out this function
/*
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc. that aren't in use.
}*/
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[tableView dealloc];
[super dealloc];
}
#pragma mark -
#pragma mark TableView DataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self.persons count];
}
/*- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier] autorelease];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [persons objectAtIndex:row];
return cell;
}*/
#end
It would be easier to help if you post your code using markdown formatting as Moshe commented. I did notice a few things with a quick scan. I also can't tell from this what you have created in interface builder and if the UITabBarController and all outlets are properly configured. You may have other things going on besides the following but here's a start.
Make sure you release anything you retain. for example, in viewDidLoad you allocate array and never release it.
Similarly, don't release things you haven't retained. In initPersons you create the mutable array aPersons using an array constructor that returns an autoreleased object. You then have [aPersons release]. This will cause a crash b/c you are releasing an object you haven't retained.
Clean up properly in viewDidUnload and dealloc. In both of these you need to release tableView. In dealloc you have [tableView dealloc]. That should be [tableView release]
Your data source creation is overly complicated but I do see a clear problem. You are setting self.persons in initPersons which is fine. You then sort the array and store it in sortedArray. Assuming your intent is to keep the array sorted, you are currently discarding the sorted array. self.persons remains the unsorted version. While this is not a crash, I doubt this was your intent.
You need to implement tableView:cellForRowAtIndexPath: This will absolutely cause a crash if missing. That's assuming you have the tableView delegate and dataSource configured correctly. You should add the protocols UITableViewDataSource and UITableViewDelegate to the interface definition for SecondViewController so you'll get the proper compiler warnings regarding implementing required protocol methods.
– tableView:cellForRowAtIndexPath: is required method of UITableviewDatasource protocol.so enable it.be happy
if u dont want any implementaion in it then just leave it with blank definition.
{
return cell;
}

Working through exercises in "Cocoa Programming for Mac OS X" - I'm stumped

I've been working through the exercises in a book recommended here on stackoverflow, however I've run into a problem and after three days of banging my head on the wall, I think I need some help.
I'm working through the "Speakline" exercise where we add a TableView to the interface and the table will display the "voices" that you can choose for the text to speech aspect of the program.
I am having two problems that I can't seem to get to the bottom of:
I get the following error: *** Illegal NSTableView data source (). Must implement numberOfRowsInTableView: and tableView:objectValueForTableColumn:row:
The tableView that is supposed to display the voices comes up blank
I have a feeling that both of these problems are related.
I'm including my interface code here:
#import <Cocoa/Cocoa.h>
#interface AppController : NSObject <NSSpeechSynthesizerDelegate, NSTableViewDelegate>
{
IBOutlet NSTextField *textField;
NSSpeechSynthesizer *speechSynth;
IBOutlet NSButton *stopButton;
IBOutlet NSButton *startButton;
IBOutlet NSTableView *tableView;
NSArray *voiceList;
}
- (IBAction)sayIt:(id)sender;
- (IBAction)stopIt:(id)sender;
#end
And my implementation code here:
#import "AppController.h"
#implementation AppController
- (id)init
{
[super init];
//Log to help me understand what is happening
NSLog(#"init");
speechSynth = [[NSSpeechSynthesizer alloc] initWithVoice:nil];
[speechSynth setDelegate:self];
voiceList = [[NSSpeechSynthesizer availableVoices] retain];
return self;
}
- (IBAction)sayIt:(id)sender
{
NSString *string = [[textField stringValue] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
//Is the string zero-length?
if([string length] == 0) {
NSLog(#"String from %# is a string with a length of %d.", textField, [string length]);
[speechSynth startSpeakingString:#"Please enter a phrase first."];
}
[speechSynth startSpeakingString:string];
NSLog(#"Started to say: %#", string);
[stopButton setEnabled:YES];
[startButton setEnabled:NO];
}
- (IBAction)stopIt:(id)sender
{
NSLog(#"Stopping...");
[speechSynth stopSpeaking];
}
- (void) speechSynthesizer:(NSSpeechSynthesizer *)sender didFinishSpeaking:(BOOL)complete
{
NSLog(#"Complete = %d", complete);
[stopButton setEnabled:NO];
[startButton setEnabled:YES];
}
- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView
{
return [voiceList count];
}
- (id)tableView: (NSTableView *)tv objecValueForTableColumn: (NSTableColumn *)tableColumn
row:(NSInteger)row
{
NSString *v = [voiceList objectAtIndex:row];
NSLog(#"v = %#",v);
NSDictionary *dict = [NSSpeechSynthesizer attributesForVoice:v];
return [dict objectForKey:NSVoiceName];
}
/*
- (BOOL)respondsToSelector:(SEL)aSelector
{
NSString *methodName = NSStringFromSelector(aSelector);
NSLog(#"respondsToSelector: %#", methodName);
return [super respondsToSelector:aSelector];
}
*/
#end
Hopefully, you guys can see something obvious that I've missed.
Thank you!
objecValueForTableColumn is not the same as objectValueForTableColumn. When it comes to delegates and data sources, I recommend never typing the method names if you can avoid it - it causes exactly this kind of problem. If you copy & paste the method signature out of the documentation you can be safer. Good luck with your learning!