Good Design for Contact Apps - Custom Table Cell Layout - objective-c

Note: After reading, please edit title if not appropriate.
I'm just amateur in iOS. I practicing the objective-C with self-task and I'm trying to create a Contact Apps without any backend.
Problem:
In my app, I would like to display name and image in a cell. How can I achieve this?. I've already tried by adding to the sub view, the label(For Name) and image view (For Image) to a cell. But, it gives more conditional check for each of the different contact types. Like...
//In cellForRowAtIndexPath,
UILabel *lblForCell=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
lblForCell.text=[arrForMyContacts objectAtIndex:indexPath.row];
[tblCellForContactTable.contentView addSubview:lblForCell];
if([lblForCell.text isEqual:#"Mom"]){
UIImageView *imgForContact=[[UIImageView alloc] initWithFrame:CGRectMake(200, 10, 60, 7)];
imgForContact.image=[UIImage imageNamed:#"Mom.png"];
[tblCellForContactTable.contentView addSubview: imgForContact];
}
//likewise condition increased for dad, bro etc...- bad design, Correct?
So tell me its there any other way to decrease condition.

There are several ways to do. First create an array of Dictionary. Something like this
NSMutableArray *contacts = [[NSMutableArray alloc] init];
NSDictionary *mom = [NSDictionary dictionaryWithObjectsAndKeys:#"MOM", #"Name",
#"mom.png", #"imageName",
nil];
NSDictionary *dad = [NSDictionary dictionaryWithObjectsAndKeys:#"DAD", #"Name",
#"dad.png", #"imageName",
nil];
NSDictionary *son = [NSDictionary dictionaryWithObjectsAndKeys:#"SON", #"Name",
#"son.png", #"imageName",
nil];
[contacts addObjectsFromArray:#[mom, dad, son]];
Second create a custom UIView class where you can render this dictionary with init method something like this
-(id)initWithContactInfo:(NSDictionary *)contactInfo;
Third at the tableView
//In cellForRowAtIndexPath,
NSDictionary *contact = [contacts objectAtIndex:indexpath.row];
MYCustomView *contactCell = [[MYCustomView alloc] initWithContactInfo:contact];
[cell.contentView addSubView:contactCell];
Making a custom View gives you flexibility of customizing your view for particular cell.
I hope it helps . Best of luck

Related

UIImagePickerController to show only videos

Is it possible to have the UIImagePickerController only show the videos on the phone and not both videos and photos? Thanks.
Set the mediaType property of the UIImagePickerController.
//Example from documentation
myImagePickerController.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil];
or to simplify the documentation's example with newer syntax:
myImagePickerController.mediaTypes = #[kUTTypeMovie];
More information and guidance is available in the documentation.

attributes not saving with file

This is probably easy, but I can not seam to figure it out - maybe it's late. I have a simple program that takes the text from an NSTextView and saves it as rtf. Saving the text itself works great, I just can not figure out how to get the attributes to tag along.
Code:
NSAttributedString *saveString = [[NSAttributedString alloc]
initWithString:[textView string]];
NSData *writeResults = [saveString
RTFFromRange:NSMakeRange:(0, [saveString length])
doumentAttributes:?? ];
[writeResults writeToURL:[panel URL] atomically: YES];
I know I need an NSDictionary for the documentAttributes, so how do I get that from the view?
What am I missing?
It seems that you are asking the textView for its string property. You need to ask it for its attributedString property:
NSAttributedString *saveString = textView.attributedString;
You can get the attributes from an attributed string like this:
NSMutableDictionary *allAttributes = [[NSMutableDictionary alloc] init];
[saveString enumerateAttribuesInRange:NSMakeRange(0,saveString.length) options:NSAttributedStringEnumerationReverse usingBlock:^(NSDictionary *attrs, NSRange range, BOOL *stop) {
[allAttrubutes addEntriesFromDictionary:attrs];
}];
NSData *writeResults = [saveString.string RTFFromRange:NSMakeRange(0,saveString.length) documentAttributes:allAttributes];
I have used this method to get attributes many times however I have never saved to RTF so I don't know exactly how this will turn out. All the attributes will be in the dictionary however.

Limit Social Sharing Options and Set Custom Message for Each Service

I am about to implement a share button calling the default modal sharing activities view.
Here's what I got so far.
NSString *textToShare = [NSString stringWithFormat:#"Looking forward to meet you at %#",[eventItemObject eventName]];;
NSURL *url = [eventItemObject eventWebsiteURL];
NSArray *activityItems = [[NSArray alloc] initWithObjects:textToShare,url,nil];
UIActivity *activity = [[UIActivity alloc] init];
NSArray *applicationActivities = [[NSArray alloc] initWithObjects:activity, nil];
UIActivityViewController *activityVC =
[[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:applicationActivities];
[self presentViewController:activityVC animated:YES completion:nil];
This is called when I press the sharing button.
Challenge:
Limit the sharing option to e.g. Facebook, Twitter, Mail (It now also shows Copy to Clipboard and Weibo)
Assign custom sharing messages/strings for each sharing method. (now the textToShare string is used for all services)
After some research I think this can be done with the new SLComposeViewController (in iOS6), but I am not sure where/how to call it correctly. Any practical advice or example is appreciated!
You can specify which activities not to show be using the activityViewController's setExcludedActivityTypes:. Example:
[activityVC setExcludedActivityTypes:[NSArray arrayWithObjects:
UIActivityTypeMessage,UIActivityTypeCopyToPasteboard,
UIActivityTypeAssignToContact,UIActivityTypePostToWeibo,
UIActivityTypePrint,UIActivityTypeSaveToCameraRoll,
nil]];
As far as sending specific content to different activities goes, I've spent the last day or so trying to figure out how to do that. I'll update this post to include this info if/when I figure it out.

xcode with IBAction

I’m trying to make an app in xcode. I have made a class called myApp.m and .h
In my .m I have these lines of code
- (void)loadApp
AlarmItem *item1 = [[[AlarmItem alloc] initWithTitle:#"TEST2"] autorelease];
NSMutableArray *items = [NSMutableArray arrayWithObjects:item1, nil];
RootViewController *rootController = (RootViewController *) [navigationController.viewControllers objectAtIndex:0];
rootController.items = items;
and in my RootViewController I have an this method:
- (IBAction)RefreshMyApp:(id)sender {
MyApp *myApp2 = [[[MyAppalloc] init] autorelease];
[myApp2 loadData];
}
What I’m trying to do is calling the method from the myApp class and displayed in the tableView, but I always get an empty cell.
Any help is appreciated.
Is this meant to get you your UIApplication singleton? (i'm guessing MyAppalloc is a typo and should be MyApp alloc)
MyApp *myApp2 = [[[MyApp alloc] init] autorelease];
if so then you should be doing it like this:
MyApp *myApp2 = (MyApp*)[UIApplication sharedApplication];
If this is not the case you need to make it clearer what MyApp is (your app delegate?)
I guess, your application running in singleton instance, if this is something kind of NSView or a particular control you want to refresh, you could call its particular refresh method like,
[NSTableView reload];
[NSTextField setString];
etc...

Memory Leak when using for(object in array) with iPhone SDK

I am running into some serious memory leaks in one of my applications I am building. I have a UINavigatonController that is inside a UITabBarview. Inside the NavView is a MKMap view. When you click an accessory button on a callout a detail view is loaded. In that detail view I am trying to populate a table from a plist using a for(object in array) loop. The plist is an array of dictionaries. I am running though the dictionaries to find one with a key that is the title of the callout and then get an array from inside that dictionary. It all works fine in the simulaor but I am getting massive memory leaks doing it the way I am. Any Idea whats going on?
- (void)viewDidLoad {
self.title = #"Route Details";
NSString *path = [[NSBundle mainBundle] pathForResource:#"stopLocation" ofType:#"plist"];
holderArray = [[NSMutableArray alloc] initWithContentsOfFile:path];
[self getRouteArray];
routeDetails.delegate = self;
routeDetails.dataSource = self;
}
-(void)getRouteArray{
for (NSMutableDictionary *dictionary in holderArray) {
//NSString *stopName = [dictionary objectForKey:#"StopName"];
//NSString *stopName = [[NSString alloc] initWithString:[dictionary objectForKey:#"StopName"]];
BOOL testString = [currentRoute isEqualToString:[dictionary objectForKey:#"StopName"]];
if (testString) {
routeArray = [[NSMutableArray alloc] initWithArray:[dictionary objectForKey:#"RouteService"]];
}
}
}
- (void)dealloc {
[routeArray release];
[routeDetails release];
[super dealloc];
}
holderArray is an ivar and so is route array. As you can see I have tried a few ways of allocating the nstrings and arrays but all seem to yield the same leaks. According to the performance tool I am leaking from NSCFString, NSCFDictionary, and the NSCFArry. I released the routeArray in the dealloc and it works fine, but if I release holderArray it crashes whenever I go back to my map from the detail view. I guess I am just really unsure as to how to deal with the strings and dictionary used in the for loop.
Just to add the detail view is being created like so:
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control{
NSString *selectedRouteName = [NSString stringWithFormat:#"%#",view.annotation.title];
RouteDetailView *rdc = [[RouteDetailView alloc] initWithNibName:#"RouteDetailView" bundle:nil];
[rdc setCurrentRoute:selectedRouteName];
[self.navigationController pushViewController:rdc animated:YES];
[rdc release];
}
Sorry if any of the above is unclear. Let me know and I can try to rephrase it.
Will testString be true for at most one key in holderArray? If so, you should probably break out of the loop after setting routeArray. If not, then you may be setting routeArray multiple times, and all but the last array you assigned to it would be leaked.
Also, I don't see you releasing holderArray.