Displaying next data in Detailview of my NSMutableArray - objective-c

I have a simple problem.
Im using a NSMutableArray for multiple Questions and Answers. I`ve put all the data in a tableView and put the data of the selected row in a Label. Now, how do i get the data from the next row using a button? in think i need something like this:
question.text = [questions objectAtIndex:indexPath.row+1];
But where to put it? If i use this line outside the cellForRowAtIndexPath Method it won't accept the indexPath.
I`m sure there is an easier way than this but with the words of Frank Sinatra: It is my way. :-)
Here is the complete methode:
- (NSInteger)tableView : (UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [questions count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"questionCell";
UITableViewCell *cell = [questionTableView
dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:simpleTableIdentifier];
}
if ([content.text isEqualToString:#"Unfallverhütung"]) {
question.text = [questions objectAtIndex:indexPath.row];
}
return cell;
}
question is the Name of my Label.
questionsis the Name of the Array.
I`m greatful for any advice.
THX

When you say
it won't accept the indexPath
What you mean is
I don't have the indexPath
So, you need to store it. You can just add a property, like:
#property (strong, nonatomic) NSIndexPath *selectedIndexPath;

Related

didSelectRowAtIndexPath to assign values

I'm having problems with a , I guess, simple thing.
I have a (It is NOT a SplitViewController) ViewController with a TableView object in it.
I want that when the users click a row in a table, my label on the right side change appropriately.
My label (with the text "Nome do Vinho" on the right) that i want to change
#property (weak, nonatomic) IBOutlet UILabel *rotuloDetalhesLabel;
I populate the TableView with this code.
I create the objects on viewDidLoad method and put in a NSArray called "vinhos".
// Create object
Vinho *vinho1 = [Vinho new];
vinho1.rotulo = #"Adega de Borda Premium";
Vinho *vinho2 = [Vinho new];
vinho2.rotulo = #"Adega de Borda Premium 2";
// Populate the NSArray with the objects that I create before
vinhos = [NSArray arrayWithObjects: vinho1, vinho2, nil];
Populating the TableView
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Configura a Cell
static NSString *CellIdentifier = #"TableCell";
TableCell *cell = (TableCell *)[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[TableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Vinho *vinho = [vinhos objectAtIndex:indexPath.row];
// rotuloLabel is the Label of the row
cell.rotuloLabel.text = vinho.rotulo;
return cell;
}
I guess that it is made on didSelectRowAtIndexPath method but I dont know how.
If I forget to post something important just tell me.
Do you wan't to change the label on the right to the cell text?
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
Vinho *vinho = [vinhos objectAtIndex:indexPath.row];
NSString *cellRotuloText = vinho.rotulo;
rotuloDetalhesLabel.text = cellRotuloText;
}
If you wan't the fade animation as you said in the comment, maybe this could get you started: xCode ios5: How to fade out a label text after an interval?

Issue when getting data from text field in dynamic cell

I have created a dynamic table, but I'm facing an issue when I try to access the content of a text field in the first cell. The textfield I get is always empty, even though I enter some text in it before calling the action.
Here is my code. Could you help me to find what's wrong in it ?
CustomNameCell.h
#interface CustomNameCell : UITableViewCell
#property (retain, nonatomic) IBOutlet UITextField *nameTextField;
#end
CustomViewController.h
#interface CustomViewController : UITableViewController
#property (retain, nonatomic) IBOutlet UITableView *tableview;
- (IBAction)search:(id)sender;
#end
CustomViewController.m :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 1) {
static NSString *CellIdentifier = #"nameCell";
CustomNameCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==nil) {
cell = [[CustomNameCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
return cell;
}
...
and the action when clicking a button, where I try to get the content of the textfield :
- (IBAction)search:(id)sender {
static NSString *CellIdentifier = #"nameCell";
CustomNameCell *nameCell = [_tableview dequeueReusableCellWithIdentifier:CellIdentifier];
here nameCell.nameTextField.text is always equal to #""
Why is my text field always empty ?
Thank you for your help ;)
If you need a cell you should get it like this:
CustomNameCell * cell = (CustomNameCell *)[tableview cellForRowAtIndexPath:indexPath];
NSString * textInField = cell.nameTextField.text;
If you tell me what logic do you need I'll expand my question. For now - you can use this method.
Content of the first cell will be:
CustomNameCell * cell = (CustomNameCell *)[tableview cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
NSString * textInField = cell.nameTextField.text;
You shouldn't call [_tableview dequeueReusableCellWithIdentifier:CellIdentifier];within your - (IBAction)search:(id)sender as it is a mechanism to reuse a cell during the creation of your tableView. Instead of creating new cell when you are scrolling your tableview you are reusing some cells you have previously created.
Bottom line: use instead the cellForRowAtIndexPath to access a cell at a specific row in your section and if you want the first cell of the first section it would be:
CustomNameCell * cell = (CustomNameCell *)[tableview cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
I just do in this Way. I dont know it is the current way to do that.
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return cellContentArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellId=#"cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];
if (cell==nil) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
}
UITextField *firsttext=[cell viewWithTag:101];
UITextField *second=[cell viewWithTag:102];
[second setTag:(indexPath.row)+1000];
[firsttext setTag:(indexPath.row)+100];
return cell;
}
- (IBAction)buttonPressed:(id)sender
{
for (int i=0; i<cellContentArray.count; i++) {
UITableViewCell * cell = (UITableViewCell *)[baseTableveiew cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
UITextField *tempTextField=[cell viewWithTag:i+100];
UITextField *secondTextField=[cell viewWithTag:i+1000];
NSString * textInField = tempTextField.text;
NSLog(#"tempTExtTiedl tag %ld data %#",(long)tempTextField.tag, textInField);
NSLog(#"second tag %ld data %#",(long)secondTextField.tag, secondTextField.text);
}
}

How to populate multiple table view sections from the same .plist

Sorry if this has been discussed already, I couldn't find what I was after..
I have a .plist file which has 2 arrays in it, which are split exactly how I would like my sections split in my table view.
I have no problem getting an array into a table view, but I cant get my head around how to tell the app that I want one array in the first section and the second array in the second section.
My code at the moment to get an array into a table is this (and it works fine):
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//Create string with reference to the prototype cell created in storyboard
static NSString *CellIdentifier = #"PlayerNameCell";
//Create table view cell using the correct cell type
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
//Create a dictionary containing the player names
NSDictionary *players = (NSDictionary*) [[self Squad] objectAtIndex:[indexPath row]];
//Set the cell text to the player name
[[cell textLabel] setText:(NSString*)[players valueForKey:#"PlayerFullName"]];
//Set the cell detail text to the squad number
[[cell detailTextLabel] setText:(NSString*)[players valueForKey:#"PlayerSquadNumber"]];
return cell;
}
But now I have another table view where I'll need 2 sections, each reading from different arrays.
Any help would be greatly appreciated.
Many thanks
Just do the following:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)aTableView
{
return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"PlayerNameCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if( cell == nil ){
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
if( indexPath.section == 0 ){
NSDictionary *players = (NSDictionary*) [self.array1 objectAtIndex:indexPath.row];
cell.textLabel.text = (NSString*)[players valueForKey:#"PlayerFullName"];
cell.detailTextLabel.text = (NSString*)[players valueForKey:#"PlayerSquadNumber"];
} else {
NSDictionary *players = (NSDictionary*) [self.array2 objectAtIndex:indexPath.row];
cell.textLabel.text = (NSString*)[players valueForKey:#"PlayerFullName"];
cell.detailTextLabel.text = (NSString*)[players valueForKey:#"PlayerSquadNumber"];
}
return cell;
}
Set the number of sections to 2. For each section get the different values with [self.array2 objectAtIndex:indexPath.row].
I don't know how you are saving the plist into 2 arrays, but if you need help with that let me know.
Okay, so you have 2 arrays at the top level, and each of those arrays contains arrays, right?
You've got a couple methods you need to adjust. Return the number of top level arrays for the number of sections in the table view. In cellForRowAtIndexPath:, return the correct object for the correct section/row. Something like
[[sectionsArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]

Can't identify if tableView parameter is seachResultsTableView

I'm developing an iOS 6 application.
I want to add a search functionallity to a UITableView following this Table Search example from Apple. But, my implementation doesn't work.
On - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath I don't know how to if tableView parameter is searchDisplayController.searchResultsTableView.
Take a look at this screenshot:
As you can see, tableView is a UISearchResultsTableView, but this line:
if ([tableView isEqual:self.searchDisplayController.searchResultsTableView])
It is always false.
Maybe, I need to add an outlet to access seachResultsTableView, but I haven't found that outlet on TableSearch example.
This ViewController.h:
#interface ViewController: UIViewController <UITableViewDelegate, UITableViewDataSource, UISearchDisplayDelegate, UISearchBarDelegate> {
What do I need to do to make it work?
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if(searching)
{
cell.textLabel.text = [searchedListOfItems objectAtIndex:indexPath.row];
}
else {
NSDictionary *dictionary = [self.tableDataSource objectAtIndex:indexPath.row];
cell.textLabel.text = [dictionary objectForKey:#"Title"];
}
return cell;
if all you're trying to achieve is a normal search then use this.

Why is my UITableView not being updated?

Why does my UITableView not update? Here is how I am attempting to update it.
- (void)viewWillAppear:(BOOL)animated
{
NSArray* arrValues = [self.defaults objectForKey:#"values"];
[self.tableScores insertRowsAtIndexPaths:arrValues withRowAnimation:UITableViewRowAnimationNone];
}
arrValues is now an array of NSNumbers. I am sure that it is not empty.
Call [tableScores reloadData]; in - (void)viewWillAppear:(BOOL)animated
Update 1
Also, you need to define arrValues in your header. Each time the viewWillAppear, you are creating a new instance, but you won't be able to use it throughout the rest of your controller. This is the main reason you aren't seeing anything besides at your breakpoint.
Update 2
According to your comment below, you have not implemented cellForRowAtIndexPath: which is how the cell is created. Below is an example, but you may want to search around the net for example projects because this UITableView's 101. There is still a lot more you need to learn when it comes to arrays and tableViews.
Example cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"FriendCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [arrValues objectAtIndex:indexPath.row];
return cell;
}