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

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
}

Related

In UITableView,cellForRowAtIndexPath ignore the first few rows

I've used sqlite in my project and I get ten elements in _mChats (array) successfully. The numberOfRowsInSection method returns 10 but cellForRowAtIndexPath ignores the first few rows and in simulator other elements all display on the same cell,why?
And if I use sections instead of rows to display data,everything becomes normal.
This is my code:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_mChats count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
VChat *cell = [VChat vChatWithTableView:tableView];
cell.mChat = _mChats[indexPath.section];
return cell;
}
I think its because of this
cell.mChat = _mChats[indexPath.section];
Try this instead
cell.mChat = _mChats[indexPath.row];
This is because Indexpath is a property that has two values inside it,
Eg. IndexPath [section,row] meaning indexpath of something is section number 1, row number 5.
Hope this helps you understand.
This is how your cellForRowAtIndexPath should look like:
- (UITableViewCell *)tableView:(UITableView *)iTableView cellForRowAtIndexPath:(NSIndexPath *)iIndexPath {
NSString *cellIdentifier = #"myCell";
UITableViewCell *cell = (UITableViewCell *)[iTableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
}
cell.mChat = _mChats[indexPath.row];
return cell;
}
- (UITableViewCell *)tableView:(UITableView *)iTableView cellForRowAtIndexPath:(NSIndexPath *)iIndexPath
{
VChat *cell = (VChat *)[tableViewdequeueReusableCellWithIdentifier:#"CellIdentifier"];
NSArray *nib = [[NSBundle mainBundle]loadNibNamed:#"VChat" owner:self options:nil];
if (cell==nil)
{
cell = nib[0];
}
cell.mChat = _mChats[indexPath.row];
return cell;
}
The three delegate method must be implement you have wrote have something wrong. As #Abhinav said the method return cell have some thing wrong.
As the code you showed will create a new cell when a new cell present in the screen. I think the first few rows just because the cell has't be reused.
There is another thing I want to say, if you are using storyboard to build the tableview this method should seems like:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// The identifier in storyboard you setted.
static NSString *cellName = #"theCell";
Cell *cell = [tableView dequeueReusableCellWithIdentifier:cellName];
if (dataSource_[indexPath.row]) {
// This method will bind data to view
[cell fillDataWithDict:dataSource_[indexPath.row]];
}
return cell;
}

Tableview data shown on label from table view

I am new in iOS development and i want to know when i click on row of table, my data which are shown in table view should be select on label. My label has name City.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [arrayname objectAtIndex:indexPath.row];
return cell;
}
In viewDidLoad add this array
dataArr=[[NSMutableArray alloc]initWithObjects:#"Hyderabad",#"Bangalore",#"Chennai",#"Pune",#"Mumbai", nil];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *reUseid=#"cellID";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:reUseid];
if (cell==nil)
{
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reUseid];
}
cell.textLabel.text = dataArr[indexPath.row];
return cell;
}
Here is TableView didSelectRowAtIndexPath method.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"selected is %#",[dataArr objectAtIndex:indexPath.row]);
[self performSegueWithIdentifier:#"viewToDescriptionSegue" sender:self];
}
in this method you got indexpath from that you can get selected row like indexpath.row and from that you can get object from array.
There is a UITableViewDelegate method named didSelectRowAtIndexPath which get called up when user select any row. Get the value from your array at indexPath.row and set the text on your label.
Implement the below code on your project:-
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *name = [arrayname objectAtIndex:indexPath.row];
[yourLabelInstance setText:name];
}
This is a delegate method of UITableView which is called when you tap on cell.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
city.text = [arrayname objectAtIndex:indexPath.row];
}
Below is a delegate method of UITableView which is called when you touch/select any row in table.
Here, using indexPath parameter you can access currently selected row index. You can access data from datasource array having selected row index.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *strCity = [arrCity objectAtIndex:indexPath.row];
[lblCity setText:strCity];
}

Any ideas how to fix this weird tableview separator line formatting issue

I'm attaching an image because this is kinda hard to explain, I've also mocked up what I actually want it to look like. I'm going to try to explain it to so that others might find the question.
Maybe you've run up against this before and know a fix?
Here is the image of what I have alongside a mockup of what I want:
On the left, what I have, the items in the table are not separated by separator lines but the empty lines cells are separated. Ideally, the empty cells would not be shown and the table would just end letting the background show and the items in the table would have separators between their cells.
This is the corresponding code:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
NSMutableArray *withoutThing = [NSMutableArray arrayWithArray:self.containerOfLists];
[withoutThing removeObjectAtIndex:indexPath.row];
self.containerOfLists = withoutThing;
[[NSUserDefaults standardUserDefaults] setObject:self.containerOfLists forKey:#"list of lists"];
[[NSUserDefaults standardUserDefaults] synchronize];
[self.tableView reloadData];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.containerOfLists count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"ListCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.text = self.containerOfLists[indexPath.row];
return cell;
}
Ok.
In the Storyboard, select your UITableView, set the Separator to Single Line, or by code with:
[yourTableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
In your code, do somewhere:
[yourTableView setBackgroundColor:[UIColor lightGrayColor]];
Or the gray color you want.
Add this to your datasources methods (as the same level as tableView: cellForRowAtIndexPath):
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
return [[UIView alloc] init];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5; //return correct number
}
in ViewDidLoad method call:
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
Init cell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"ListCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (!cell)
{
//init the cell!
}
cell.textLabel.text = self.containerOfLists[indexPath.row];
return cell;
}

Not able to populate second UITableView Controller

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

i want to number my rows in a UITableView

im using core data i have an entity called questions whenever i add my question to my table i want them to display the number of the question (kind of like auto increment thing)
can anyone pls help me out
thanks
here is my code if its relevant
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.questionArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
// Configure the cell...
questionObject = [self.questionArray objectAtIndex:indexPath.row];
cell.textLabel.text = questionObject.questionDescription;
return cell;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if (nil == cell) {
cell = ...; //create a cell instance if it wasn't found in reusable cache
}
// Configure the cell...
questionObject = [self.questionArray objectAtIndex:indexPath.row];
NSString *cellText = [NSString stringWithFormat:#"%d. %#", indexPath.row+1, questionObject.questionDescription];
cell.textLabel.text = cellText;
return cell;
}

Resources