My question is that is it possible to get text data and read it in my ViewController from php when the cell is tapped?
I'm guessing didSelectRowAtIndexPath is the key to do this, but I'm not sure how to write.
I am able to get data from php into my TableViewController as below;
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [json count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
NSDictionary *info = [json objectAtIndex:indexPath.row];
cell.textLabel.text = [info objectForKey:#"Artist"];
cell.detailTextLabel.text = [info objectForKey:#"Song"];
return cell;
}
The code you have written kind of contradicts what you explained in the beginning. However, This is how it should be done:
First off you will need to get the data you want to display. You can get this from an external website (php, as you call it), plist, xml or whatever you want. You can do this when you have your tableview initiated (e.g. using ASIHTTPRequest) or before you initiate your tableview and just send it in using a custom init method.
Second, you need to read the contents you have recieved. Usually it great to get is in a NSArray format. This depends on your implementation of the data source you are fetching data from.
If you are new to objective-c and cocoa-touch, you should start off with some quick tutorials regarding UITableView. I have written one myself, but currently I guess its (very) outdated. You could still have a look at it though: http://paulpeelen.com/2009/08/14/developing-with-iphone-3-0-x-simple-uitableview/
Your code is I think wrong. Check if there any data in json by checking
NSLog(#"%# and %#",[info objectForKey:#"Artist"], [info objectForKey:#"Song"]);
and tell me if any result comes out. if not then there is some mistake in your php else your data should show there.
use this method of code.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//retrive your data here.
}
Related
I've tried so many things and I'm totally at a loss. My text isn't appearing in my cell's default text labels in TableView. The strange part is everything else does work. By that I mean, I can read and parse the data source (XML), I can create the right amount of rows (5), and I can even get the cells to contain the text data. I know this because when I use NSLog it shows this. I even double check by using didSelectRowAtIndexPath and NSLog(#"%#",cellText); and the right text is in the log! So the cell text label contains the text I want, but it's still not appearing on the screen. Instead, I just get 5 blank rows that are each selectable, and will display the right text in the NSLog when I select them but not on the screen.
I've tried the following fixes: adjusting backgroundColor and textColor (nope), trying different sample code I could find online (nope), using ViewControllers instead of TableViewControllers (nope), deleting the files and re-building them (nope), and even creating new projects with only this code to isolate the code (nope). Every time, it's still blank! I have no idea what to do.
Here's the code I'm using to grab and display the data in the tableview:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [app.listArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
theList = [app.listArray objectAtIndex:indexPath.row];
cell.textLabel.text = theList.name;
NSLog(#"%#",cell.textLabel.text);
NSLog(#"%#",theList.name);
return cell;
}
I assume that this is a new plain project that does not have anything else that could interfere. You checked with NSLog(#"%#",cell.textLabel.text) that the text is populated, you verified that the background and foreground colors are not the same. One remaining thing to check is transparency/Alpha. Make sure it is set to 1.
If this is on the simulator, try testing on a device. If you can't test on a device, try different simulator device to rule out the possibility that it is a simulator glitch.
Also, try hardcoding the label text to something like #"Abcdef" to make sure that the source of the culprit is not the data you are trying to display.
Try like this way. see how I wrote cellForRowAtIndexPath method.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [listArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
[cell.titleLabel setText:[listArray objectAtIndex:indexPath.row]];
NSLog(#"%#",cell.textLabel.text);
NSLog(#"%#",theList.name);
return cell;
}
I had this same problem but the answer to my problem was the Connection Inspector on the right hand side for the cell. The Outlet needs to be set to accessoryView for the ContentView. Just drag it over to the cell. Don't feel bad I pulled my hair out too.
Im using Xcode7 Beta and Swift
On my UITableViewI am using custom UITableViewCells. Each of these cells has a number of labels. When the user selects a cell, I need to capture the contents of just one of these labels, but I don't know how to do that. Here is my code. The line that I am using to attempt to get this label text is basically pseudo-code that clearly won't compile. Can somebody please tell me what I need to do here? Thanks!
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
...
Groups *group = [self.fetchedObjects objectAtIndex:indexPath.row];
cell.groupDescriptionLabel.text = group.group_descr;
cell.groupIDLabel.text = [group.group_id stringValue];
return cell;
}
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// capture the user selection
Groups *group = [self.fetchedObjects objectAtIndex:indexPath.row];
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
NSString *selection = selectedCell.groupDescriptionLabel.text; //<-- pseudo-code
NSLog(#"%#", group.group_descr);
...
}
It's generally a bad idea to get data out of the view. You shouldn't really be using any view objects as a way of storing information.
You are already getting the string when you are in the cellForRowAtIndexPath method.
You should be able to do the same in didSelectRowAtIndexPath to get the same string.
That way you don't have to get the text out of the label at all.
Thanks
If you know that celForRowAtIndexPath is going to be returning one of your custom cell types instead of a generic UITableViewCell, cast the result to your custom cell class:
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// capture the user selection
MyCellClass *selectedCell = (MyCellClass *) [tableView cellForRowAtIndexPath:indexPath];
NSString *selection = selectedCell.groupDescriptionLabel.text; //<-- pseudo-code
NSLog(#"%#", selection);
//...
}
I'm parsing a bunch of json objects from my API and place it into a table view. When I hit the a row I want it to parse an object from the API out of an array and forward it to another view so one can get more details.
If I place the code how I think it works in the cellForRowAtIndexPath everything is fine but by the time I place an NSLog in the didSelectRowAtIndexPath and make him display the ID I parsed for the specific row I don't get anything?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *neuheitenCell = #"neuheitenCell";
CJHomeTableCell *cell = [tableView dequeueReusableCellWithIdentifier:neuheitenCell];
sstring = [[idArray objectAtIndex:indexPath.row] objectForKey:#"ware_id"];
//NSLog(#"Selected item with id: %#",sstring);
return cell;
}
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"stuff: %#", sstring);
}
Thanks in advance
Because you used didDeselectRowAtIndexPath: instead of didSelectRowAtIndexPath:.
Note the spelling.
I am from C# background and now learning objective C.
I created two NSArrays 1.optionsLabelArray and 2.optionsLabelSubtitleArray initially with two elements each (optionsLabelArray=>Yes & No) and optionsLabelSubtitleArray(acceptable and action). Now requirement changed and I have to add one more item and I am doing it like below. But It still shows only two elements.
I tried using NSMutableArray , no luck. Looks like it's a small issue but couldn't figure it out.. any ideas will be appreciated.
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
optionsLabelArray = [[NSArray alloc] initWithObjects:#"Yes", #"No",#"No" ,nil]; // here is problem
optionsLabelSubtitleArray = [[NSArray alloc] initWithObjects:#"Acceptable",#"Action required",#"No", nil]; // here to
static NSString *CellIdentifier = #"CheckerCell";
CustomCell *cell =[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.optionLabel.text = [optionsLabelArray objectAtIndex:indexPath.row];
cell.optionLabelSubtitle.text = [optionsLabelSubtitleArray objectAtIndex:indexPath.row];
return cell;
}
You need to update the implementation of your - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section method to support more rows. To do so you probably want to have an implementation where the array is either an instance variable of the class or otherwise consistent between the two methods.
you need implement following method and return 3 when you increase cell count
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
see more on https://developer.apple.com/library/ios/documentation/uikit/reference/UITableViewDataSource_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/UITableViewDataSource/tableView:numberOfRowsInSection:
I am creating a simple app to select video urls out of a UITable. I have hooked my data source and delegate to a UIViewController subclass and the table is filled correctly. Also, it recognizes selections and prints to the log.
My issue is that it gets a "EXC_BAD_ACCESS" and crashes when I select a cell. I am looking through the code and the error propagates to this method:
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString* cellIdentifier = #"SelectionIdentifier";
//Errors start happening this next line
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
}
//NSString* str = [[videos objectAtIndex:[indexPath row]] lastPathComponent];
NSString* test = #"test";
[[cell textLabel] setText:test];
return cell;
}
-(void) tableView:(UITableView*)myTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// NSLog(#"Selected!");
}
-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {
return [videos count];
}
I'm not sure why this error is getting thrown in this method. Any fixes? I double checked to make sure the videos array wasn't nill.
I did another app that used this same method and it doesn't cause these errors.
Any help or suggestions would be greatly appreciated!
Instead of testing if(cell == nil) try using if(!cell). Honestly I'm not sure this is the issue, but after reviewing this I do not think that the error is not actually inside this method (it may somehow be related which is why it brings you here).
If this is only after you select a cell though, why is this method being called?
I think you should also call this method.Because this is preliminary delegate method
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
After seeing your tableView i am not finding any problem at all.Try that may be it will be solve.