- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"Abelia"]) {
if ([[segue destinationViewController] isKindOfClass:[SpeciesViewController class]]) {
SpeciesViewController *Species = (SpeciesViewController *)[segue destinationViewController];
Species.SpeciesInt = 0;
}
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section
{
if (SpeciesInt == 0)
return [abeliaArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"SpeciesNameCell";
SpeciesNameCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[SpeciesNameCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"SpeciesNameCell"];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
}
if (SpeciesInt == 0)
cell.SpeciesNameLabel.text = [abeliaArray objectAtIndex:indexPath.row];
return cell;
}
Im not sure where the problem in this 3 methods, it runs but the abeliaArray will run in place of the other arrays when I try to push. I have other arrays but when I push those the objects in the abeliaArray will show instead of the objects for that array.
Related
#import "MasterTableViewController.h"
#interface MasterTableViewController ()
#end
#implementation MasterTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = self.editButtonItem;
[self.tableView setDelegate:self];
[self.tableView setDataSource:self];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.tableView reloadData];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self isEditing] ? self.wishListItems.count + 1 : self.wishListItems.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
WishListItem *item = self.wishListItems[indexPath.row];
if (indexPath.row >= [self.wishListItems count] && self.tableView.isEditing) {
cell.textLabel.text = #"New subject";
cell.imageView.image = nil;
} else {
cell.textLabel.text = item.name;
cell.imageView.image = item.photo;
}
return cell;
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
if (editing) {
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:#[[NSIndexPath indexPathForRow:self.wishListItems.count inSection:0]] withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView endUpdates];
} else {
[self.tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:#[[NSIndexPath indexPathForRow:self.wishListItems.count inSection:0]] withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView endUpdates];
}
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.wishListItems removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView reloadData];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
WishListItem *newItem = [[WishListItem alloc] initWithName:#"New subject" photo:nil price:0.0 andNotes:#"Empty"];
[self.wishListItems insertObject:newItem atIndex:indexPath.row];
[tableView insertRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView reloadData];
}
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row >= [self.wishListItems count]) {
return UITableViewCellEditingStyleInsert;
} else {
return UITableViewCellEditingStyleDelete;
}
}
//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// [self.tableView deselectRowAtIndexPath:indexPath animated:true];
// if (indexPath.row >= self.wishListItems.count && self.editing) {
// [self tableView:tableView commitEditingStyle:UITableViewCellEditingStyleInsert forRowAtIndexPath:indexPath];
// }
//}
#pragma mark - Navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"showDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
WishListItem *item = self.wishListItems[indexPath.row];
DetailViewController *detailViewController = (DetailViewController *)segue.destinationViewController;
detailViewController.item = item;
}
}
#end
I changed "number of row in section" and inserted new row but issue
" index 3 beyond bounds [0 .. 2] ", and I changed "number of row in section"
without increment, appear issue " reason: 'attempt to insert row 3 into section 0, but there are only 3 rows in section 0 after the update' "
I double-check a thousand times and still can not find the problem, help who was able to find what is the problem in the code.
In cellForRowAtIndexPath:, you're accessing your array before you do your bounds check, so it will crash when your table is editing.
Only do the array access after the bounds check:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
if (indexPath.row >= [self.wishListItems count] && self.tableView.isEditing) {
cell.textLabel.text = #"New subject";
cell.imageView.image = nil;
} else {
WishListItem *item = self.wishListItems[indexPath.row];
cell.textLabel.text = item.name;
cell.imageView.image = item.photo;
}
return cell;
}
I have a custom cell with few views and buttons in it. From 1 of the buttons, I'm performing a Segue and I need that cell number but no idea how to get it. Please help.
Heres how my cell looks:
the prepareForSegue is called from the red circled button.
Here's the code:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return _array.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NewsFeedCell *cell = [tableView dequeueReusableCellWithIdentifier:#"NewsFeedCell" forIndexPath:indexPath];
if (cell) {
cell.item = _array[indexPath.section];
}
return cell;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"AudioComment"])
{
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
NSLog(#"%ld",(long)indexPath.section); //printing 0 everytime :(
}
}
Please provide any help/suggestions on how can i get the indexPath?
Thanks.
You can use
UITableViewCell *cell = (UITableViewCell *)[[sender superview]superview];
NSLog(#"%ld",(long)[_generalTableView indexPathForCell:cell].row);
You have to set tag on the button. In your case, set the button tag as indexPath.section in CellforRowAtIndexPath.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NewsFeedCell *cell = [tableView dequeueReusableCellWithIdentifier:#"NewsFeedCell" forIndexPath:indexPath];
if (cell) {
cell.item = _array[indexPath.section];
}
cell.button1.tag = indexPath.section;
return cell;
}
In PrepareForSegue, get the index from button tag.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"AudioComment"])
{
NSInteger index = ((UIButton *) sender).tag;
NSLog(#" %ld ",index);
}
}
Add a variable for the index and set it when selecting the cell. Access the variable in perpareForSegue, pass it to the destinationVC and 0 it.
Use below code. It may help you.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MainPhotoCell *cell = (MainPhotoCell *)[tableView dequeueReusableCellWithIdentifier:#"CellIdentifierMedia" forIndexPath:indexPath];
if (!cell)
{
cell = [[MainPhotoCell alloc] init];
}
cell.usrCommentBtn.tag = indexPath.row;
[cell.usrCommentBtn addTarget:self action:#selector(onCommentClick:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
- (void)onCommentClick:(id)sender
{
if ([GlobalManager checkUserLoginStatus:self])
{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:([sender tag]) inSection:0];
[self performSegueWithIdentifier:#"showComments" sender:indexPath];
}
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString: #"showComments"])
{
NSIndexPath * path = (NSIndexPath *)sender;
//do the your required code here.
}
}
I am trying to pass data from one table view Cell to next UIViewController . I am using Navigation Controller also , But when i am clicking Cell then it giving error :-
-[UIViewController setSelectedRowTitle:]: unrecognized selector sent to instance 0x13fab360
my prepareForSegue code which i used go next with data is:-
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [NearBy_PlacesArray count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *TableIdentifire = #"NearMe";
NearByTableViewCell *cell = (NearByTableViewCell*)[self.tableView dequeueReusableCellWithIdentifier:TableIdentifire];
if (cell == nil)
{
cell = [[NearByTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TableIdentifire];
}
NearByPlace *nearBy = [NearBy_PlacesArray objectAtIndex:indexPath.row];
cell.NearBy_Thumbnail_Image.image = [UIImage imageNamed:nearBy.NearByImageName];
cell.NearBy_TitleLabel.text = nearBy.NearByPlaceName;
cell.NearBy_NoLabel.text = nearBy.NearByPlace_no;
return cell;
}
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"ShowNearByPlaces"])
{
NSIndexPath *indexPath = nil;
NearByPlace *nearPlace = nil;
indexPath = [self.tableView indexPathForSelectedRow];
nearPlace = [NearBy_PlacesArray objectAtIndex:indexPath.row];
NSLog(#"indexPath-- %# \n" , indexPath);
NSLog(#"recipe-- %# \n " , nearPlace);
nearByPlaceDetail_ViewController *destViewController = segue.destinationViewController;
destViewController.SelectedRowTitle = nearPlace;
}
}
Mantosh, you can simply do this :
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *aStrIdentifier = #"cell";
UITableViewCell *aCell=[tableView dequeueReusableCellWithIdentifier:aStrIdentifier];
UILabel *aLblPlaceName=(UILabel*)[aCell viewWithTag:1];
aLblPlaceName.text=[NSString stringWithFormat:#"%#", [arrPlaces objectAtIndex:indexPath.row]];
return aCell;
}
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
strStoreValue = [NSString stringWithFormat:#"%#", [arrPlaces objectAtIndex:indexPath.row]];
[self performSegueWithIdentifier:#"detailViewSegue" sender:self];
}
(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
DetailViewController *aObjDetailVC = segue.destinationViewController;
aObjDetailVC.strPlace = strStoreValue;
}
So I was trying to combine the search bar with the Nav bar for tableview in iOS7.
I called
self.searchDisplayController.displaysSearchBarInNavigationBar = YES; And it looks fine.
However, when I tap anywhere in the tableview, the search bar is activated. There's essentially no way I can regularly click a table cell.
I'm wondering what exactly did I miss here?
And for reference the code related to tableview looks like this: (I used storyboard to handle click logic)
#pragma mark - Table view delegate method
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [[[UILocalizedIndexedCollation currentCollation] sectionTitles] objectAtIndex:section];
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return [[UILocalizedIndexedCollation currentCollation] sectionIndexTitles];
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
return [[UILocalizedIndexedCollation currentCollation] sectionForSectionIndexTitleAtIndex:index];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
if (tableView == self.searchDisplayController.searchResultsTableView) {
return 1;
} else {
return [[[UILocalizedIndexedCollation currentCollation] sectionTitles] count];;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.searchDisplayController.searchResultsTableView) {
return [searchResults count];
} else {
return [self.contacts[section] count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"contactCell";
UITableViewCell *cell;
Contact *contact;
if (tableView == self.searchDisplayController.searchResultsTableView) {
cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
contact = searchResults[indexPath.row];
} else {
cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
contact = [[self.contacts objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
}
UILabel *nameLabel = (UILabel *)[cell viewWithTag:7];
UIImageView *starMark = (UIImageView *)[cell viewWithTag:9];
NSString *nameString = [NSString stringWithFormat:#"%# %#",contact.firstName, contact.lastName];
if ([contact.star isEqual:#0]) {
starMark.hidden = YES;
} else {
starMark.hidden = NO;
}
[nameLabel setText:nameString];
return cell;
}
And the segue method is defined here:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"pushContactDetail"]) {
NSIndexPath *indexPath;
Contact *contact;
if (self.searchDisplayController.active == YES) {
indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
contact = searchResults[indexPath.row];
} else {
indexPath = [self.tableView indexPathForSelectedRow];
contact = [[self.contacts objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
}
//get indexPath from selected sushi
//initialize the detail view controller and push it
CIContactViewController *destViewController = segue.destinationViewController;
destViewController.contact = contact;
}
}
I'm unable to populate the second UITableView Controller, wondering if anyone could help.
I'm using a websites API, JSON, and RestKit for the data. I believe that part is working fine because my first VC loads fine.
But I'm not sure if I need to use prepareForSegue and/or didSelectRowAtIndexPath so that I can identify the cell/row selected in the first VC, so that the second VC populates with the (correct) data.
1st VC populates correct:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return sports.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
Sport *sport = [sports objectAtIndex:section];
return sport.leagues.count;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
Sport *sport = [sports objectAtIndex:section];
return sport.name;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"standardCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
Sport *sport = [sports objectAtIndex:indexPath.section];
League *league = [sport.leagues objectAtIndex:indexPath.row];
cell.textLabel.text = league.name;
return cell;
}
2nd VC populates blank table:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return leagues.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
League *league = [leagues objectAtIndex:section];
return league.teams.count;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
League *league = [leagues objectAtIndex:section];
return league.name;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"standardCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
League *league = [leagues objectAtIndex:indexPath.section];
Team *team = [league.teams objectAtIndex:indexPath.row];
cell.textLabel.text = team.name;
return cell;
}
Or if I try to add extra code for 1st VC, app crashes before getting to 2nd VC:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
TeamsViewController *teamsViewController = [[TeamsViewController alloc] initWithNibName:#"TeamsViewController" bundle:nil];
teamsViewController.title = [[sports objectAtIndex:indexPath.row] objectForKey:#"sports"];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"leagueDetail"]) {
TeamsViewController *tvc = [segue destinationViewController];
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
tvc.data = [self.navigationController objectInListAtIndex:indexPath.row]
}
Would really appreciate any help!
I am a little bit confused with excepting of creation UITableViewCell object. When you ask table view for dequeuing cell it returns one which it does not need at the moment, if there are no unused cell you have to create a new one. Try code like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Create cell
NSString *cellIdentifier = #"cell";
UITableViewCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:cellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
}
cell.textLabel.text = [NSString stringWithFormat:#"cell %d", indexPath.row];
return cell;
}