How do I access UITableView section label from didSelectRowAtIndexPath - xcode

In my tableView didSelectRowAtIndexPath I can access the cell textLabel. How do I access the section label?
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
topPlacesAppDelegate *appDelegate = (topPlacesAppDelegate *) [[UIApplication sharedApplication] delegate];
appDelegate.chosenPhoto = [self.topPlaces objectAtIndex:indexPath.row];
. . .
UITableViewCell *mycell = [tableView cellForRowAtIndexPath:indexPath];
NSLog(#" indexPath.section / Row = %i / %i", indexPath.section, indexPath.row);
NSLog(#" textLabel.text = %#", mycell.textLabel.text);
NSLog(#" detailTextLabel.text = %#", mycell.detailTextLabel.text);
[self performSegueWithIdentifier:#"Show Site Table" sender:nil];
}

It sounds like you are looking for the table section header. I don't think you can read or otherwise access that data. But you can set it with the method on the UITableViewDataSource:
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
Details are described here:
Change UITableView section header/footer WHILE RUNNING the app?

Related

How to push new view from TableViewCell?

In my app I am trying to navigate to a new View from a TableViewCell, But i dont know how. Could you help guys? I am using Xcode 4.6. and .xib, no storyboard. I am very new to coding so please explain it as simple as possible! Here is what i have tried, please correct me:
- (void)viewDidLoad
{
tableData = [[NSArray alloc] initWithObjects:#"aaoversikt1", #"Paul", #"George", #"Ringo", nil];
[super viewDidLoad];
UILabel *nav_title1 = [[UILabel alloc] initWithFrame:CGRectMake(60, 10, 220, 25)];
nav_title1.font = [UIFont fontWithName:#"Arial-BoldMT" size:18];
nav_title1.textColor = [UIColor whiteColor];
nav_title1.adjustsFontSizeToFitWidth = NO;
nav_title1.textAlignment = NSTextAlignmentCenter;
nav_title1.text = #"Ă…lesund";
nav_title1.backgroundColor = [UIColor clearColor];
self.navigationItem.titleView = nav_title1;
}
#pragma mark - TableView Data Source methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section;
{
return [tableData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
UITableViewCell *cell = nil;
static NSString *CellIdentifier = #"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
return cell;
}
#pragma mark - TableView Delegate methods
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
AAOversiktViewController *aaoversikt1 = [[AAOversiktViewController alloc] initWithNibName:#"AAOversiktViewController" bundle:nil];
[self.navigationController pushViewController:aaoversikt1 animated:YES];
}
Thank You!
From first glance I would say that the problem lies within your [self.navigationController pushViewController] method. Have you initiated a navigation controller in your app either in the app delegate or root view controller?
If not i would recommend:
[self presentViewController:aaoversik1 animated:YES completion:NULL];
If you are wanting to push through the navigation stack so that you have a back button then I would look at implementing a UINaviationController.
Otherwise you can create a custom back button and use the same method in this post to present the root view controller (not totally recomended as using UINavigationController is much cleaner).
Let me know if this has been of any help, T

Xcode parsing RSS XML with images issue

I am trying to parse text with images in the UITableView and I can only see the text for every line but I can't see the images.
Here is my code;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [self.anaTablo dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
UILabel *textBaslik = (UILabel *) [cell viewWithTag:3];
UIImage *imagePic = (UIImage *) [cell viewWithTag:7];
textBaslik.text =[titleArray objectAtIndex:indexPath.row];
imagePic.images = [imageArray objectAtIndex:indexPath.row];
return cell;
}
but it didn't works and after searching I have tried this to;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
UILabel *textBaslik = (UILabel *) [cell viewWithTag:3];
UIImage *imagePic = (UIImage *) [cell viewWithTag:7];
textBaslik.text =[titleArray objectAtIndex:indexPath.row];
NSString *str1 = [[NSString alloc] initWithFormat:#"%#", imageArray];
NSData *bgImageData = [NSData dataWithContentsOfFile:str1];
UIImage *img1 = [UIImage imageWithData:bgImageData];
imagePic = [img1 objectAtIndex:indexPath.row];
return cell
}
Am I doing something wrong? I am kind new to Xcode
Thanks from now.
I check you code and i change the following
NSString *str1 = [[NSString alloc] initWithFormat:#"%#", imageArray];
NSData *bgImageData = [NSData dataWithContentsOfFile:str1];
UIImage *img1 = [UIImage imageWithData:bgImageData];
imagePic = [imageArray objectAtIndex:indexPath.row];
cell.imageView.image=imagePic;
in the delegate method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
Now a new error appear, check it
Check this tutorial about parsing RSS with XMLParses, it works perfectly.
Wiki XML parse data
You should check this sample code using TBXML framework
TBXML parser

Xcode: How to switch to another window using TableView?

the error ---> when i click the name the application crashes...
rectification---> when i click the name in table it should jump to another view...
my code is
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[ListArr objectAtIndex:indexPath.row];
ListData *oList = [[ListData alloc] initWithNibName:#"ListData" bundle:nil];
NSString *psCatId = [NSString stringWithFormat:#"%d",indexPath.row+1];
oList.psId = psCatId;
[self presentModalViewController:oList animated:YES];
[oList release];
}

mwfeedpaser detailview

I just started using xcode and OBJ-c this year.
I am using mwfeedparser for a app I'm making but would like to change the look of the detailview.
right now i'm using the demo app that came with mwfeedparser to change the detail view.
i would like the detail view to have labels instead of being a tableview
i have changed this code
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
DetailViewController *detail = [[DetailViewController alloc] initWithNibName:#"DetailView" bundle:nil];
detail.item = (MWFeedItem *)[itemsToDisplay objectAtIndex:indexPath.row];
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detail animated:YES];
// Deselect
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}
when i run the code the detailview doesn't even come up. am I doing something wrong?
thanks for your help.
btw when i do get this done I'll put it up on github incase anybody else needs it
enter code hereself.parsedItems = [[NSMutableArray alloc] init];
- (void)feedParser:(MWFeedParser *)parser didParseFeedItem:(MWFeedItem *)item{
if (item) {
[self.parsedItems addObject:item];
}
}
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DetailViewController *detail = [[DetailViewController alloc] initWithNibName:#"DetailView" bundle:nil];
detail.item = [self.parsedItems objectAtIndex:indexPath.row];
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detail animated:YES];
}

Grouped Table View

New to Xcode,
I have created a grouped table view,containing different amounts of rows in each section and I now do not know the best way to populate the cells with data then move on to the next view when the cell is pressed.
Please Help.
Thank you
you could try out this basic tutorial to see how you can populate your table..
http://blog.webscale.co.in/?p=150
and for performing action when a cell is pressed, you would basically need to do something like this in the didSelectRowAtIndexPath method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if( 0 == indexPath.row)
{
NSLog(#"first row selected");
if(patientView == nil)
patientView = [[PatientList alloc]init];
[self.navigationController pushViewController:patientView animated:YES];
}
This is just a very very basic example of what you want to do... playing a bit around with this would help you learn more...
**set in .h file**
#property(nonatomic, retain)NSArray *data;
#property(nonatomic, retain)NSArray *sections;
**in .m file view did load**
self.sections = [[NSArray alloc] initWithObjects:#"university",#"sabaragamuwa",#"Srilanka",nil];
self.data = [[NSArray alloc]initWithObjects:
[[NSArray alloc] initWithObjects:
#"CiS",
#"006",
#"07", nil],
[[NSArray alloc] initWithObjects:
#"cis",
#"006",
#"007", nil],
[[NSArray alloc] initWithObjects:
#"cis",
#"006",
#"07", nil],nil];
**addthoose**
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
// Return the number of sections.
return [self.sections count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return [self.sections objectAtIndex:section];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
// Return the number of rows in the section.
return [data count];}
**addalsooo**
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
SimpleTableIdentifier];
// Configure the cell...
if (cell == nil){
// Create the cell.
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier];
}
NSArray* dataArray = [data objectAtIndex:indexPath.section];
cell.textLabel.text = [dataArray objectAtIndex:indexPath.row];
cell.textLabel.textAlignment = UITextAlignmentRight;
cell.textLabel.textColor = [UIColor colorWithRed:0.0 green:139.0/255.0 blue:69.0/255.0 alpha:1.0];
UIImage *cellImage = [UIImage imageNamed:#"oranimCellIco.png"];
cell.imageView.image = cellImage;
return cell;}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSArray* dataArray = [data objectAtIndex:indexPath.section];
NSString *catgory= [dataArray objectAtIndex:indexPath.row];
if(catgory==#"cis")
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Answer Set"
message:#"Answer set as Yes Or No"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
}
else if(catgory==#"006")
{
answerviewcon *vc1=[self.storyboard instantiateViewControllerWithIdentifier:#"answerviewcon"];
[self presentViewController:vc1 animated:YES completion:nil];
}

Resources