UITableView issues - iphone

Iam adding objects to NSMutableArray and displaying the value in UITableView. Only the first object of the NSMutableArray is getting displayed.Where am i going wrong?
My Code snippet:
-(IBAction) segmentedControlIndexChanged:(id)sender {
int selectedSegment = ((UISegmentedControl*) sender).selectedSegmentIndex;
switch (selectedSegment) {
case 1: {
MatchSchedule *semiFinal1 = [[MatchSchedule alloc] initWithDeails:#"20 March"];
MatchSchedule *semiFinal2 = [[MatchSchedule alloc] initWithDeails:#"24 March"];
matchList = [[NSMutableArray alloc] initWithObjects:semiFinal1,semiFinal2,nil];
[semiFinal1 release];
[semiFinal2 release];
break;
}
default;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
...
....
MatchSchedule *matchSchedule = [knockoutStage objectAtIndex:indexPath.row];
cell.textLabel.text = matchSchedule.matchDate;
}

Hard to tell without seeing more of your code. Are you returning the correct value for
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [knockoutStage count];
}
?

Related

UITableView indexPathForSelectedRow return 0 (incorrect value)

I'm trying to make an upcoming events table in my app but when I select a row indexPathForSelectedRow always returns 0.
NSArray *upcommingTitle;
NSMutableArray *upcommingSubtitle;
NSMutableArray *upcommingTime;
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)datesTable numberOfRowsInSection:(NSInteger)section
{
return upcommingTitle.count;
}
-(UITableViewCell *)tableView:(UITableView *)datesTable cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"Cell";
DatesCustomCell *Cell = [datesTable dequeueReusableCellWithIdentifier:cellIdentifier];
if(!Cell)
{
Cell = [[DatesCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
Cell.DatesTitle.text = [upcommingTitle objectAtIndex:indexPath.row];
Cell.DatesSubtitle.text = [upcommingSubtitle objectAtIndex:indexPath.row];
Cell.DatesTime.text = [upcommingTime objectAtIndex:indexPath.row];
return Cell;
}
self.datesTable.dataSource = self;
self.datesTable.delegate = self;
View Did Load
upcommingTitle = [[NSMutableArray alloc] initWithObjects:#"Title1", #"Title2", nil];
upcommingSubtitle = [[NSMutableArray alloc] initWithObjects:#"Sub1", #"Sub2", nil];
upcommingTime = [[NSMutableArray alloc] initWithObjects:#"Date1", #"Date2", nil];
But the following always returns 0 resulting in the "test" label to be "Title1"
View Did Appear
_test.text = [upcommingTitle objectAtIndex:self.datesTable.indexPathForSelectedRow.row];
Thanks for you help
try to push your another view in your didSelect method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Here, indexpath returns the selection of cell's indexpath, so put your code here
_test.text = [upcommingTitle objectAtIndex:indexPath.row];
//then push your view
}
UPDATED : then use this code
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([[segue identifier]isEqualToString:#"pushView"])
{
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
PushingViewController *pushView = [segue destinationViewController];
pushView._test.text = [upcommingTitle objectAtIndex:indexPath.row];
}
}
(void)tableView:(UITableView *)datesTable didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
_test.text = [upcommingTitle objectAtIndex:indexPath.row];
}
Just put this code in your application.
Try not to deselect cell before presenting your seque
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//Call This Line
[self performSegueWithIdentifier:#"ShowMobileShopStore" sender:nil];
//Before This
[tableView deselectRowAtIndexPath:indexPath animated:true];
}
The initial question was: indexPathForSelectedRow return 0 (incorrect value),
Well, this is by-design, from the apple developer notes, the value returns an the IndexPath which can be NULL (is what you are seeing).
So, to do things right:
1) You need to test if the indexPathForSelectedRow is NOT NULL first
2) If it's not NULL then it would contain (.row) which would then give you the selected row-index, this starts from 0.
3) Please note that indexPathForSelectedRow.row would return ZERO in the case of indexPathForSelectedRow is NULL.
Here is the code that I've tested and works:
if (MyTableViewList.indexPathForSelectedRow)
{
long SelectedRow = MyTableViewList.indexPathForSelectedRow.row;
}
I hope this helps,

Accessing UITextField in UITableViewCell

I'm using a UITableView with a subclassed prototype cell with a UITextField. The cell is used 4 times total in 4 sections. This is the code for it:
...
- (void)viewDidLoad
{
[super viewDidLoad];
cellTitles = [[NSArray alloc] initWithObjects:#"Smell", #"Taste", #"Suits", #"Notes", nil];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [cellTitles count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [cellTitles count] / [cellTitles count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [cellTitles objectAtIndex:section];
}
- (AddInfoCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"AddInfoCell";
AddInfoCell *addInfoCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (addInfoCell == nil) {
addInfoCell = [[AddInfoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
addInfoCell.cellTextView.delegate = self;
return addInfoCell;
}
I want to add the text from each cell to the dataSource object:
-(IBAction)nextButtonPressed:(id)sender {
...
[[dataSource objectAtIndex:dataSourceIndex] setObject:* forKey:#"Smellnote"];
[[dataSource objectAtIndex:dataSourceIndex] setObject:** forKey:#"Tastenote"];
//etc
…
}
*text from cell 1
**text from cell 2
How can I access the text that I want?
Maybe you're looking at it wrong. Why not make the delegate for the textField be the cell itself, ie, in the Cell's class, set the delegate to File'sOwner or self in the init... method.
Create a reference to the dataSource and pass the dataSource to your cell, along with the #"Key" for your dictionary.
cell.dataSource = self.dictionary;
cell.dataKey = #"Tastenote";
//in cell .m
//textFieldDelegate method...
[self.dataSource setObject:self.cellTextView.text forKey:self.dataKey];

UITableView Splitting contents into Sections

I have a UITableView for which I have listed the code below.
How do I section these objects out such as the following shown below?
Math
Algebra
Geometry
Calculus
Science
Integrated 1
Integrated 2
Here is the code:
- (void)viewDidLoad {
NSArray *array = [[NSArray alloc] initWithObjects:#"Math",#"Science",#"English",#"Social Studies",#"Spanish",#"German",#"French",#"Biology",nil];
self.listData = array;
[array release];
[super viewDidLoad];
}
You have to use sectioned table view, that is the best way to achieve this. You can refer following tutorial for the same - http://www.xcode-tutorials.com/uitableview-–-3-sectioned-table-view/
In your UITableViewDataSource of UITableView object;
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return array.count;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [array objectAtIndex:section];
}
will do the 'sectioning' part for your case.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [yourarray count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [yourarray objectAtIndex:section];
}

How to pass data back from detail view controller to uitableview?

In my app i making use of uitable to select category from my list.
my task is,when ever user click or select a cell he should be able to view the selected cell detail in next view(detail view). and when he select the item in a detail view he should be able to move back in a table view and should be able to see the selected item in a rootivew controller.
i am able to navigate properly from tableview to detail view,but i am not able to show the item which is selected in detail view to rootviewcontroller.
please help me out with this issue.
image one is my rootview controller page.
for example : if user select #"make" he will able to see all the releated category of #"make"
. in a next page(which image2).
image to is my detail page.
and when user select #"abarth" it should be display in a rootview controller page(which is page one).
following is my code of rootview controller page:
- (void)viewDidLoad
{
self.car = [[NSArray alloc]initWithObjects:#"Make",#"Model",#"Price Min",#"Price Max",#"State",nil];
[super viewDidLoad];
}
-(NSInteger) numberOfSectionInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.car count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *TextCellIdentifier = #"Cell";
UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:TextCellIdentifier];
if (cell==nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TextCellIdentifier];
cell.accessoryType = UITableViewCellAccessoryNone;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
cell.textLabel.text = [self.car objectAtIndex:[indexPath row]];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (0 == indexPath.row)
{
NSLog(#"0");
self.detailcontroller.title = #"Make";
}
else if (1 == indexPath.row)
{
NSLog(#"1");
self.detailcontroller.title = #"Model";
}
else if (2 == indexPath.row)
{
NSLog(#"2");
self.detailcontroller.title = #"Price Min";
}
else if (3 == indexPath.row)
{
self.detailcontroller.title = #"Price Max";
}
else if (4 == indexPath.row)
{
NSLog(#"3");
self.detailcontroller.title = #"State";
}
[self.navigationController
pushViewController:self.detailcontroller
animated:YES];
}
following is my detail view page code:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if ([self.title isEqualToString:#"Make"])
{
detail = [[NSArray alloc]initWithObjects:#"Any Make",#"Abarth",#"AC",#"ADAYER",#"Adelaide",#"ALFA ROMEO",#"ALLARD",#"ALPINE-RENAULT",#"ALVIS",#"ARMSTRONG",
#"ASTON MARTIN",#"AUDI",#"AUSTIN",#"AUSTIN HEALEY",#"Barossa",#"BEDFORD",
#"BENTLEY",#"BERTONE",#"BMW",#"BOLWELL",#"BRISTOL",#"BUICK",#"BULLET",
#"CADILLAC",#"CATERHAM",#"CHERY",#"CHEVROLET",#"CHRYSLER",#"CITROEN",
#"Country Central",#"CSV",#"CUSTOM",#"DAEWOO",#"DAIHATSU",#"DAIMLER",
#"DATSUN",#"DE TOMASO",#"DELOREAN",#"DODGE",#"ELFIN",#"ESSEX",
#"EUNOS",#"EXCALIBUR",#"FERRARI",nil];
if ([self.title isEqualToString:#"Abarth"])
{
detail = [[NSArray alloc]initWithObjects:#"HI", nil];
}
}
else if ([self.title isEqualToString:#"Model"])
{
detail = [[NSArray alloc]initWithObjects:#"Any Model", nil];
}
else if ([self.title isEqualToString:#"Price Min"])
{
detail = [[NSArray alloc]initWithObjects:#"Min",#"$2,500",
#"$5,000",
#"$7,500",
#"$10,000",
#"$15,000",
#"$20,000",
#"$25,000",
#"$30,000",
#"$35,000",
#"$40,000",
#"$45,000",
#"$50,000",
#"$60,000",
#"$70,000",
#"$80,000",
#"$90,000",
#"$100,000",
#"$500,000",
#"$1,000,000",nil];
}
else if ([self.title isEqualToString:#"Price Max"])
{
detail = [[NSArray alloc]initWithObjects:#"Max",
#"$2,500",
#"$5,000",
#"$7,500",
#"$10,000",
#"$15,000",
#"$20,000",
#"$25,000",
#"$30,000",
#"$35,000",
#"$40,000",
#"$45,000",
#"$50,000",
#"$60,000",
#"$70,000",
#"$80,000",
#"$90,000",
#"$100,000",
#"$500,000",
#"$1,000,000",nil];
}
else if ([self.title isEqualToString:#"State"])
{
detail = [[NSArray alloc]initWithObjects:#"Any State",
#"Australian Capital Territory",
#"New South Wales",
#"Northern Territory",
#"Queensland",
#"South Australia",
#"Tasmania",
#"Victoria",
#"Western Australia",nil];
}
[self.tableView reloadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [detail count];
}
- (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];
}
cell.textLabel.text = [detail objectAtIndex:
[indexPath row]];
cell.font = [UIFont systemFontOfSize:14.0];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.navigationController popViewControllerAnimated:YES];
}
You need to make use of custom delegates. Create a protocol in your detailview and implement it in your rootview.Pass the selected string as parameter to delegate method and from the delegate method, display it in your textfield.
//something like this
#interface detailViewController
// protocol declaration
#protocol myDelegate
#optional
-(void)selectedValueIs:(NSString *)value;
// set it as the property
#property (nonatomic, assign) id<myDelegate> selectedValueDelegate;
// in your implementation class synthesize it and call the delegate method
#implementation detailViewController
#synthesize selectedValueDelegate
// in your didselectRowAtIndex method call this delegate method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self selectedValueDelegate])selectedValueIs:valueString] ;
[self.navigationController popViewControllerAnimated:YES];
}
#end
// In your rootViewController conform to this protocol and then set the delegate
detailViewCtrlObj.selectedValueDelegate=self;
//Implement the delegate Method
-(void)selectedValueIs:(NSString *)value{
{
// do whatever you want with the value string
}
Hi you will have to do it using protocols and delegate
Please see my linkon protocol and delegate
you can also do it by creating a variable in appdelegate , setting its property and accessing it in any other view .
YourAppDelegate *appDelegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate];
delegate.yourVariable;

trouble with section headers in UITableView

im having a problem with setting my section headers in a uitableview, its probably something really simple i just cant work it out.
instead of displaying different headers for different sections it displays the same header for each section
help me please :)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
WorldCupAppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
return [appDelegate.matchFixtures count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
WorldCupAppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
Fixtures *fixtures = [appDelegate.matchFixtures objectAtIndex:section];
return fixtures.matchDate;
}
Your original code looks okay. I'm betting that appDelegate.matchFixtures doesn't contain the data you think it does. Modify your code to look like this:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
WorldCupAppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
NSLog(#"appDelegate.matchFixtures.count = %i", appDelegate.matchFixtures.count);
return [appDelegate.matchFixtures count];
}
- (NSString *)tableView:(UITableView *)tableView
titleForHeaderInSection:(NSInteger)section {
WorldCupAppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
Fixtures *fixtures = [appDelegate.matchFixtures objectAtIndex:section];
NSLog(#"For section %i, fixtures.matchDate = %#", section, fixtures.matchDate);
return fixtures.matchDate;
}
And look at the log output in the debug console.
Try like this
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection: (NSInteger)section {
NSString *title = nil;
// Return a title or nil as appropriate for the section.
switch (section) {
case 0:
title = [[appDelegate.matchFixtures objectAtIndex:section]matchDate];
break;
case 1:
title = [[appDelegate.matchFixtures objectAtIndex:section]matchDate];
break;
case 2:
title = [[appDelegate.matchFixtures objectAtIndex:section]matchDate];
break;
default:
break;
}
return title;
}
Edit
Set the date properly with retain in the delegate class.Even i had some problems similar when storing values in delegates.
- (void)setCurrentDates:(NSString *)value {
[value retain]; // <- Retain new value
[date release]; // <- Release old value;
date = value;
}
All the best.