I am using TableViewCell in my application. and the cell data like label text and image from imageview at perticular cell. should view in next view controller.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
MainBodyViewController * mainViewController =[[MainBodyViewController alloc]init];
MasterDetails * masted =(MasterDetails*)[listArray objectAtIndex:indexPath.row];
NSLog(#"did select row at index is givne as:%#",masted);
mainViewController.headLbl.text=[NSString stringWithFormat:#"%#",masted.title];
}
But the problem is I am not getting data from listArray. The data coming from json.
You need to check
Table view No of rows should be [listarry count] to avoid out of
index.
"MasterDetails" object is created properly, as per no of cell == json parsed object.
"listarray" is allocated.
Related
I've got a table view and each time I select a new select, it logs the stats of the cell selected before it.
So let's say I select cell B, it won't log anything if that's the first cell I've selected since opening the table view. But if I then select cell A, it will log the stats of cell B.. then if I select cell C it will log cell A's stats, etc.
Any idea why?
Code below:
- (void) tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
FeedItem *feedItem = [[FeedItem alloc] init];
feedItem = [self.feedItemList objectAtIndex:indexPath.row];
NSLog(#"%# username", feedItem.username);
}
Wrong method. You want didSelectRowAtIndexPath. Yours is Deselect instead of Select
I am trying to get a URL from a cell. To do this, I am using NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; and then would like to do something like NSURL *url = self.finalURL[indexPath.row] but because indexPath.row is only for Arrays, this doesn't work. Is there a way to achieve the same thing as indexPath.row but for objects not in an array.
Here is how I am saving the url:
cell.finalURL = self.finalURL;
A cell doesn't have a URL, unless you create a subclass of the cell and add that property to is. Conventionally, you will have an array of objects, strings, dictionaries, etc., and that is your tableView's data source.
If I had an array with three NSURLs in it called myArray that contained google, amazon, and bing, and I wanted to display three cells with the respective labels matching the items in the array, I would implement the following code:
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// we only want a single section for this example
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// this tells the tableView that you will have as many cells as you have items in the myArray array
return myArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// first we try to reuse a cell (if you don't understand this google it, there's a million resources)
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
// if we were unable to reuse a cell
if (cell == nil) {
// we want to create one
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
// here is where we do ANY code that is generic to every cell, such as setting the font,
// text color, etc...
cell.textLabel.textColor = [UIColor redColor];
}
// here is where we do ANY code that is unique to each cell - traits that are based on your data source
// that you want to be different for each cell
// first get the URL object associated with this row
NSURL *URL = myArray[indexPath.row];
// then set the text label's text to the string value of the URL
cell.textLabel.text = [URL absoluteString];
// now return this freshly customized cell
return cell;
}
That, along with the rest of the default tableview code and setting up the array itself, results in the following:
When a user taps on a cell you can access the URL in the array and do something with it like so:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// first deselect the row so it doesn't stay highlighted
[tableView deselectRowAtIndexPath:indexPath animated:YES];
// get the URL associated with this cell by using indexPath.row as an index on your
// data source array, if you tapped the first cell, it will give you back the first URL in your array...
NSURL *selectedURL = myArray[indexPath.row];
// do something with that URL here...
}
Think of your table view's data source as a bunch of little cubbies. You can create the data source in a million different ways, but once you have it you basically take the items and place them in numbered cubbies. Your table view create's itself based on what's in those cubbies, so to make the first cell it looks in the first cubbie, and so on, and later on when a user selects a cell from that tableview, all the table view does is tell you the cubbie number that was selected, and it's your job to use that information to retrieve the data from that specific cubbie and do what you need to with it. Hope that helps!
I know that there is this method to detect which UITableViewCell is selected:
- (void) tableView: (UITableView *) tableView didSelectRowAtIndexPath: (NSIndexPath *) indexPath {
}
But i don't want to know the selected row, i want to know what the content of the cell is and according to that i want to do something. So is there a way to tag a cell?
For better understanding, let's say i have four cells 1, 2, 3 and 4. When i reorder them and my order is 1, 4, 2, 3 and i want to show the value in another view, then it'd show me 2 instead of 4 when i select the second row.
You cab call
- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath
method from your tableView:didSelectRowAtIndexPath: implementation to get the cell, like this:
- (void) tableView: (UITableView *) tableView didSelectRowAtIndexPath: (NSIndexPath *) indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
...
}
However, this is not the proper way of accessing, especially modifying, the table's content. Instead, you should access the model from which the table takes its information, and make all modifications there. This way the changes would "stick" when the current cell's visuals are scrolled off the screen and back.
There are three ways as far as my knowledge,
1) Access that cell by calling [tableView cellForRowAtIndexPath:indexPath].
Now you can compare data of cell with your model. i.e. You can use NSPredicate to get index value of your selected row data in you model(from which you are filling table cell)
2) When ever you reorder your table row simply change your model as well. E.g. Row-4 becomes Row-2, reorder your model(probably ArrayList) by simply remove object from 4th position and add in 2nd position. So when ever you delete table row it will be same index number of your model data for that row.
3) You can tag each view. But this is code redundancy. You can achieve such thing with out doing so. Instead of this option i will go for 1st or 2nd (For my opinion 2nd option is best if use your model/Array for keeping record only.)
You can access the selected cell like this in didselect delegate method.
UITableViewCell *lab = [tableView cellForRowAtIndexPath:indexPath];
UILabel *selectedCellLabelText = lab.textLabel.text;
Firstly you can define a property of NSString in your second class to access the value corresponding to particular cell content, arrayData is your array in which your value is stored e.g. 1, 2, 3, 4, 5 then write this code in your first class :-
- (void) tableView:(UITableView *) tableView didSelectRowAtIndexPath:(NSIndexPath *) indexPath
{
SecondViewController *secondView = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
secondView.stringValue = [arrayData objectAtIndex:indexPath.row];
[self.navigationController pushViewController:secondView animated:YES];
}
I have an app that lists some data in a tableview, in cells. I want the user to be able to select a table view cell, any one, and have the app cycle through the 5 lower cells. Here is what I have so far:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
// Navigation logic may go here. Create and push another view controller.
[self fetchCellsToProcess:indexPath];
}
Here is the fetchCellsToProcess: method:
-(void)fetchCellsToProcess:(NSIndexPath*)indexPath{
for (int cellsToProcess = 0; cellsToProcess < 5; cellsToProcess++) {
//process each cell
//...
}
}
I need use the indexPath to get its indexPath.row. Then add 5 to that indexPath.row and only process the tweets between indexPath.row passed in and indexPath.row+5. What programming logic should I use to cycle through cells x -> x+5?
You should not be using cellForRowAtIndexPath: here: that's part of the presentation logic, while you are working on the model-level logic here.
Look at your cellForRowAtIndexPath: method, and see from where does the text of the cell's labels come. Usually it is an NSArray or some other collection. Your didSelectRowAtIndexPath: method should go directly to that same collection, and grab the info from there.
There is no direct approach to achieve this as dasblinkenlight mentioned
but there is a work around which works for me.
Please follow below approach:
Create your custom button within your custom cell
Create your custom cell class and put button(change type to custom ) do necessary connection custom cell class
create and connect action method named "actionSelectedCell" as below from that button in class you are implementing this logic.
And cellForRowAtIndexPath cell.customButton.text = data from your array or dictionary
- (IBAction)actionSelectedCell:(UIButton *)sender {
// below code for getting selected cell row and its section
UIView *view = sender.superview;
while (view && ![view isKindOfClass:[UITableViewCell self]]) view = view.superview;
UITableViewCell *cell = (UITableViewCell *)view;
indexPathForSelectedCell = [self.tableViewListViewVC indexPathForCell:cell];
NSLog(#"cell is in section %d, row %d", indexPathForSelectedCell.section, indexPathForSelectedCell.row);// so now you are getting indexpath for selected cell
// now you can create simple loop logic to get required data and do whatever you like post it to something or store it for any other use
NSString *strFifthCellContent = [NSString stringWithFormat:#"%#",[dataArray ObjectAtIndex:indexPathForSelectedCell.row+5]]; // in this way you can get data from any cell
}
I have a custom table view cell with 3 fields in it. I need to get the data in one of those fields to use in a SQLite query.
I've looked at cellForRowAtIndexPath, but don't see how to address the particular cell I want (it was defined with an IBOutlet, so it has a name) and get it's value.
When the row is tapped, your didSelectRowAtIndexPath delegate method will be called. In that method, you can use [tableView cellForRowAtIndexPath:indexPath] to get the row's cell. Then you can get whatever you need out of the cell.
I figured it out... here is the code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
SingletonClass *shareInstance= [SingletonClass sharedInstance];
sArray *sa = [shareInstance.listOfSites objectAtIndex:indexPath.row]; // (rows are zero based)
NSString *labelContent = sa.sSiteID; // labelContent = site_id
// get list of sites based on selected row
slSQLite *dbCode = [[slSQLite alloc] init];
[dbCode getListOfSites:labelContent]; // labelContent is used to build query
}