iOS Cell Label link - objective-c

Hi I am new to iOS programming and I need a little help.
I have a table view cell which is populated by data from a .plist file. I need to be able to make a link within one of the cells. How could I do this?
Here is the code which is loading the data into the cells:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"CustomTableCell";
static NSString *CellNib = #"DetailViewCell";
DetailViewCell *cell = (DetailViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellNib owner:self options:nil];
cell = (DetailViewCell *)[nib objectAtIndex:0];
}
cell.accessoryType = UITableViewCellAccessoryNone;
cell.cellTitleLabel.textColor = [UIColor blackColor];
cell.cellTitleLabel.font = [UIFont systemFontOfSize:20.0];
cell.cellSubtitleLabel.textColor = [UIColor darkGrayColor];
informations = [[NSArray alloc] initWithObjects:#"City", #"Country", #"State", #"History", #"Link", nil];
subtitles = [[NSArray alloc] initWithObjects:titleString, subtitleString, stateString, populationString, #"Link", nil];
cell.cellTitleLabel.text = [informations objectAtIndex:indexPath.row];
cell.cellSubtitleLabel.text = [subtitles objectAtIndex:indexPath.row];
return (DetailViewCell *) cell;
}
For the cell "Link" I need it to open a url which is stored in the .plist file. How could i do this?
Many thanks
Ryan
PS: I'm a newbie at this stuff, so be descriptive. Thanks

Firs of all, move the following to viewDidLoad
informations = [[NSArray alloc] initWithObjects:#"City", #"Country", #"State", #"History", #"Link", nil];
subtitles = [[NSArray alloc] initWithObjects:titleString, subtitleString, stateString, populationString, #"Link", nil];
Second, for the cell that has a value of #"Link"
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
DetailViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if(cell.cellTitleLabel.text == #"Link")
{
//Open in safari
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:cell.cellSubtitleLabel.text]];
}
}

Make the two arrays properties of your viewController.
#property (nonatomic, retain) NSArray *informationArray, *subtitlesArray;
Initialise them in your -viewDidLoad method:
self.informationArray = [NSArray arrayWithObjects:#"City", #"Country", #"State", #"History", #"Link", nil];
self.subtitlesArray = [NSArray arrayWithObjects:titleString, subtitleString, stateString, populationString, #"Link", nil];
and then check to see if the retrieved data is a link, then open it in safari.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
id title = [self.informationArray objectAtIndex:indexPath.row];
NSURL *link = [NSURL URLWithString:[self.subtitlesArray objectAtIndex:indexPath.row]];
//or to get the link out of a .plist
NSDictionary *plist = [NSData dataWithContentsOfFile:filePathForPlist];
NSURL *link = [NSURL URLWithString:[[plist objectForKey:title] objectForKey:#"Link"];
// you may need to get a different object from the .plist depending on the structure of the file.
//Check to see if the indexPath matches a cell with a title of "Link" and that the URL can be opened.
if([title isEqualToString:#"Link"] && [[UIApplication sharedApplication] canOpenURL:link]) {
[[UIApplication sharedApplication] openURL:link];
}
}

Related

How Block or disable someones Customs Cells From Tableview with data from plist?

well I have 2 plist files:
And:
My StoryBoard is:
the problem is when in my "Inicio" viewcontroller load data from the plist, I want Disable the cells when have the ENABLE=NO property & Enable the cells with ENABLE = YES, when I push or click in the cell, then go to the next view controller "PERSONAS" in this view controller load the 2nd Plist and in the same case I want go to the "DETALLE"viewcontroller only with the enabled cells that have ENABLED YES from plist.
for each viewdidload Im use:
NSString *myListPath = [[NSBundle mainBundle] pathForResource:#"MainMenuList" ofType:#"plist"];
mainMenu = [[NSArray alloc]initWithContentsOfFile:myListPath];
NSLog(#"%#",mainMenu);
NSString *myListPath = [[NSBundle mainBundle] pathForResource:#"PersonasList" ofType:#"plist"];
Personas = [[NSArray alloc]initWithContentsOfFile:myListPath];
NSLog(#"%#",Personas);
And for show the tableview in the custom cell Im use:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = #"CustomCell";
NSLog(#"CARGANDO CELDAS");
MainMenuCell *cell = (MainMenuCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CustomCelliPhone" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
//CustomCell*cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
cell.Enabled.text = [[mainMenu objectAtIndex:indexPath.row]objectForKey:#"ENABLE"];
cell.TitleLabel.text = [[mainMenu objectAtIndex:indexPath.row]objectForKey:#"NAME"];
cell.ImageImageView.image = [UIImage imageNamed:[[mainMenu objectAtIndex:indexPath.row]objectForKey:#"IMAGE"]];
if ([cell.Enabled.text isEqualToString:#"NO"])
{
NSLog(#"%#",cell.Enabled.text);
cell.userInteractionEnabled = NO;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.selected = NO;
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
in the Persona view controller I use the same code but not working, all customs cells are enabled, how can I fix that?, or am I wrong in something? or I did Forget Something please help guys!!
in the Simulator run in that way for ENABLE:
But I dont wanna the cells with ENABLE NO run!!:
Please help guys, im using XCODE 5 DP6, for iOS7, how is the best way for solve this!!!
THANKS!!!!
for many intents, finally fix this!! well in each viewcontroller the code whit solve this is like:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = #"MainMenuCell";
NSLog(#"CARGANDO CELDAS");
MainMenuCell *cell = (MainMenuCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"MainMenuCelliPhone" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
//MainMenuCell*cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
cell.Enabled.text = [[mainMenu objectAtIndex:indexPath.row]objectForKey:#"Enabled"];
cell.TitleLabel.text = [[mainMenu objectAtIndex:indexPath.row]objectForKey:#"Title"];
cell.ImageImageView.image = [UIImage imageNamed:[[mainMenu objectAtIndex:indexPath.row]objectForKey:#"Image"]];
//cell.ImageImageView.image = [[mainMenu objectAtIndex:indexPath.row]objectForKey:#"Image"];
if ([[[mainMenu objectAtIndex:indexPath.row]objectForKey:#"Enabled"] isEqualToString:#"NO"])
{
NSLog(#"%#",cell.Enabled.text);
cell.userInteractionEnabled = NO;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.selected = NO;
cell.accessoryType = UITableViewCellAccessoryNone;
cell.ImageImageView.image = [UIImage imageNamed:#"NO.png"];
}
else
{
cell.ImageImageView.image = [UIImage imageNamed:#"YES.png"];
}
return cell;
//------------------------------------------//
// THANKS anyway //
// GRETTINGS FROM BOLIVIA //
// ROCK ON!!!! n_n' //
//------------------------------------------//
}

custom cell not appear when loading new views objective-C

I create a UITableView programmatically with different cells and sections that connects to the other views in storyboard
But if you check the story board absence view has custom cell with check box sections that it's not appear here
My question is:
why it doesn't shows the custom cell?,would you please helping me
Thanks in advance!
Here is my code:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString: #"WorkTime"]){
[segue.destinationViewController setTitle:#"WorkTime"];
}if([segue.identifier isEqualToString: #"Absence"]){
[segue.destinationViewController setTitle:#"Absence"];
}if([segue.identifier isEqualToString: #"Compensation"]){
[segue.destinationViewController setTitle:#"Compensation"];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableArray *keys = [[NSMutableArray alloc] init];
NSMutableDictionary *contents = [[NSMutableDictionary alloc] init];
NSString *workKey = #"work";
NSString *absKey = #"absence";
NSString *comKey = #"compensation";
[contents setObject:[NSArray arrayWithObjects:#"Work Time", nil] forKey:workKey];
[contents setObject:[NSArray arrayWithObjects:#"Absence", nil] forKey:absKey];
[contents setObject:[NSArray arrayWithObjects:#"Compensation", nil] forKey:comKey];
[keys addObject:workKey];
[keys addObject:absKey];
[keys addObject:comKey];
[self setSectionKeys:keys];
[self setSectionContents:contents];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *key = [[self sectionKeys] objectAtIndex:[indexPath section]];
NSArray *contents = [[self sectionContents] objectForKey:key];
NSString *contentForThisRow = [contents objectAtIndex:[indexPath row]];
static NSString *CellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
[[cell textLabel] setText:contentForThisRow];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
_selectedIndex = indexPath.row;
[self.tableView reloadData];
//case1
[self performSegueWithIdentifier:#"WorkTime" sender:self];
//case2
[self performSegueWithIdentifier:#"Absence" sender:self];
//case3
[self performSegueWithIdentifier:#"Compensation" sender:self];
}
I think your problem is here
static NSString *CellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
Your custom cell should have a unique cell identifier. You need to pass that unique cell identifier to -dequeueReusableCellWithIdentifier:.
What have you used as your cell identifier?
you should use NSLOG to be sure that you are going to different views, I think you are always in a same view is the reason that you cann't see the new changes

Writing a .plist based on tableView User Choice

I've got a tableView populated with twitter accounts that I want to write to a .plist when a user selects one. Currently, however, when I tap an account, it writes the textLabel.text of the bottom cell.
Here's my code.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *writablePath = [documentsDirectory stringByAppendingPathComponent:#"test.plist"];
NSMutableArray *testArray = [[NSMutableArray alloc] init];
[testArray addObject:cell.textLabel.text];
NSArray *context = [[NSMutableArray alloc] initWithObjects:#"Name", nil];
NSMutableDictionary *new = [[NSMutableDictionary alloc] initWithObjects:testArray forKeys:context];
[new writeToFile:writablePath atomically:YES];
}
I don't see where the cell object is being created at. If you want to get the cell that was just selected, you can do this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
}
This will allow you to grab the actual cell that the user 'touched'.

Objective-C, iOS SDK, NSMutableArray, add elements and save data

Basically, every time I enter something in my textfield and save it to the table, the previous entry will be replaced by the new one. How do I add elements in a mutable array and save the last entry? I tried
[tabledata addObject....
and in the entry
tabledata.lastObject objectAtIndex...
but that didn't work.
here is what I have:
-(void)viewDidLoad
{
[super viewDidLoad];
titlestring = [[NSUserDefaults standardUserDefaults] objectForKey:#"titlehomework" ];
detailsstring = [[NSUserDefaults standardUserDefaults] objectForKey:#"detailshomework"];
tabledata = [[NSMutableArray alloc] initWithObjects:titlestring, nil];
tablesubtitles = [[NSMutableArray alloc] initWithObjects:detailsstring, nil];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
return [tabledata count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
UITableViewCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:#"homeworkcell"];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"homeworkcell"];
}
cell.textLabel.text = [tabledata objectAtIndex:indexPath.row];
cell.detailTextLabel.text = [tablesubtitles objectAtIndex:indexPath.row];
cell.textLabel.font = [UIFont systemFontOfSize:14.0];
cell.textLabel.backgroundColor = [ UIColor clearColor ];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
//-----------------------------------------START----------------------------Set image of cell----
cellImage = [UIImage imageNamed:#"checkboxblank.png"];
cell.imageView.image = cellImage;
//--------------------------------------------END---------------------------end set image of cell--
return cell;
}
Without seeing more code, the best I can do is suggest something like this
[tabledata addObject:newTitle];
[tablesubtitles addObject:newSubtitle];
[tableView reloadData];
This assumes both newTitle and newSubtitle are the NSStrings you wish to add and that tableView is the pointer to the UITableView.

Cocoa Touch UITableView Data

I am working for the first time in objective c and have come across an issue that I have not seen an answer for.
I am loading a set of data from a JSON data set and using it to populate a UITableView within a UITableViewController.
First when the view is loaded (viewDidLoad) I populate the array with the JSON data from a URL.
Next the data loads as it should. numberOfRowsInSection shows that there are 30 results in the array which is correct.
However The Iphone loads the entire set three times into the tableview.
Here is some code from the controller for that view:(Twitter is being used as an example and is not the actual data set I use)
- (void)viewDidLoad {
[super viewDidLoad];
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
//results = [[NSMutableArray alloc] init];
self.navigationItem.title=#"public_timeline";
self.tableView.allowsSelection = NO;
NSString *url = [[NSString alloc] init];
url = [[NSString alloc] initWithFormat:#"http://twitter.com/statuses/%s.json",#"public_timeline"];
if ([results count] == 0) {
[self parseJSON:url];
}
}
Here is the parseJSON (actual parse is done with the Cocoa JSON framework
-(void) parseJSON:(NSString *)URL{
NSURL *JSONURL = [NSURL URLWithString:URL];
NSData *responseData = [[NSData alloc] initWithContentsOfURL:JSONURL];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
results = [[NSMutableArray alloc] initWithArray:[responseString JSONValue]];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [results count];
}
then the cell's output
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
// Set up the cell...
}
NSDictionary *cdict = [results objectAtIndex:indexPath.row];
cell.textLabel.text = [cdict valueForKey:#"text"];
return cell;
}
I am not sure if what I am doing is the best way to do this, so if someone could help me out that would be great.
Check the numberOfSectionsInTableView Method and change the return Value to 1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}