Not able to populate second UITableView Controller - ios

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;
}

Related

how to assign an array element in a indexPath from selected cells

I have an array with sound list
NSArray *nameSound = #[#"alarm-1.wav",#"alarm-2.mp3",#"alarm-3.mp3",#"alarm-4.mp3",#"alarm-5.mp3"];
And I have TableView with 5 cells,
How award for indexPath from TableView to element from nameSound
P.S. Sorry for my English
You have to pass that array in tableview with the help of below methods
//pass your array count for cell numbers
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [nameSound count];
}
//For Cell creation
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier forIndexPath:indexPath];
cell.textLabel.text = [nameSound objectAtIndex:indexPath.row];
return cell;
}
//Did Select row will get in below method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *selectedSound = [nameSound objectAtIndex:indexPath.row]
}
it will help you to bind nameSound data with table cell.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *selectedVal = [nameSound objectAtIndex:indexPath.row];
// You can use this selectedVal
}

dispalying data from NSarray to table view

I am new to iOS and I am trying to build an lightweight app for practice. I am taking two textfield entries from one view controller and storing them in NSDictionary. I am transferring those data in NSArray. Now I want to display those data from NSArray to table view. I am using this line of code:
cell.textLabel.text=[self.myArray objectAtIndex:indexPath.row];
But my app is crashing when cursor comes at this line. Help is appreciated.Thanks in advance.
For UITableView you have to define at least 2 methods:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
It seems your method numberOfRowsInSection is not implemented.
Try to add this:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:
{
return [self.myArray count];
}
Of course your ViewController have to be delegated for UITableViewDataSource and UItableViewDelegate.
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.yourArry 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];
}
//Configure Cell
cell.textLabel.text = self.yourArry[indexPath.row];
return cell;
}
Please try this code
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:
{
return [self.myArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell=nil;
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text =[self.myArray objectAtIndex:indexPath.row]
return cell;
}
Trying using a pointer to an NSString (or what object you are trying to make)
NSString *string = [self.myArray objectAtIndex:indexPath.row]
Make sure you have the count of cell in the tableview set like so
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:
{
return self.myArray.count
}
Step One.
In YourViewController.h
#import <UIKit/UIKit.h>
#interface YourViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
In YourViewController.m
#import "YourViewController.h"
#interface YourViewController ()
{
UITableView * YourTableview;
}
- (void)viewDidLoad
{
YourTableview = [[UITableView alloc] initWithFrame:CGRectMake(0,0, 300, 500) style:UITableViewStylePlain];
YourTableview.delegate=self;
YourTableview.dataSource=self;
[self.view addSubview:YourTableview];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * CellIdentifier=#"Cellid";
UITableViewCell * SoftCell=[YourTableview dequeueReusableCellWithIdentifier:CellIdentifier];
if (!SoftCell)
{
SoftCell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
textLabel.text = [NSString stringWithFormat:#"%ld",(long)indexPath.row];
return SoftCell;
}

iOS UITableViewCell Have To Hold A Long Press To Select Item

So this is driving me nuts, whenever I tap an item in my UITableView it does nothing, but when i press and hold the UITableViewCell after about 3-5 seconds it decides to move forward and do what I want.. any thoughts why this might be happening?
Here's my code
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 100;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
_cell = [_arrayItems objectAtIndex:indexPath.row];
_cell = nil;
static NSString *AutoCompleteRowIdentifier = #"AutoCompleteRowIdentifier";
_cell = (CustomWidget *)[tableView dequeueReusableCellWithIdentifier:AutoCompleteRowIdentifier];
if (_cell == nil) {
_cell = [[CustomWidget alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:AutoCompleteRowIdentifier title:[_arrayItems objectAtIndex:indexPath.row] subTitle:#"Custom subtitle"];
}
_cell.textLabel.text = [_arrayItems objectAtIndex:indexPath.row];
_cell.textLabel.hidden = YES;
return _cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _arrayItems.count;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
_passedInPageTitle = selectedCell.textLabel.text;
[self openDetailPage];
}
I found my answer here - I had a UITapGestureRecognizer on the UITableView's parent view, which was capturing the tap.

Why simple cells loads before custom cells?

I have made customCell for my UITableView. I am downloading data from server and showing on CustomCells.
The method of manipulating data is
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"CustomCellReuseID";
LeaderBoardCell *cell = [leaderBoardTable dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[LeaderBoardCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
GetScoreBoard *ob = [leaderBoardArray objectAtIndex:indexPath.row];
[[cell title] setText:ob.fullname];
[[cell discription] setText:ob.points];
return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [leaderBoardArray count];
}
The CustomCell "LeaderBoardCell" is loading later, but before this I see simple cells which are default in UITableView.
Is is possible to load only CustomCell in TableVeiw?

Doesn't work UITableViewCell's method

When I do simple HelloWorld app I have problem with
[self.tableView insertRowsAtIndexPaths:#[indexPath]withRowAnimation:UITableViewRowAnimationAutomatic]
,this method doesn't work... and i don't know why? pliz help
Code:
- (void)viewDidLoad
{
int i = 0;
[super viewDidLoad];
while (i < 10 ) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:#[indexPath]withRowAnimation:UITableViewRowAnimationAutomatic];
i++;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.text = #"HelloWorld";
return cell;
}
App should create 10 cells with Label #"HelloWorld"
You're going about this the wrong way. If you want 10 cells you shouldn't be trying to add them one at a time, you should just return 10 in numberOfRowsInSection, e.x:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 10;
}
Additionally, nothing will show up in the cells because you have call alloc/init on the cells in cellForRowAtIndexPath. Modify your code to do so like this:
- (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];
}
cell.textLabel.text = #"HelloWorld";
return cell;
}

Resources