Is it possible to find section of row where I am clicking in tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath method.
TIA
the indexPath parameter has section property accessible as following
[indexPath section];
OR
indexPath.section
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.section==0)
{
// 1st Section of the tableView.
if(indexPath.row==0)
{
// 1st cell of the 1st section.
}
}
}
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
int section = [indexPath section];
}
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:
(NSIndexPath*)indexPath{
int section = [indexPath section];
NSLog(#"section %d",section);
}
Related
Hello I'm beginner in iOS In one of my activity I want to reload table data ...then I have applied this code .....
-(void)buttonpress
{
for(int i=0;i<buttonarr.count;i++)
{
NSString *butt=[NSString stringWithFormat:#"%#",[buttonarr objectAtIndex:i]];
for(int j=0;j<LabelTag.count;j++)
{
NSString *labelbutt=[NSString stringWithFormat:#"%#",[LabelTag objectAtIndex:j]];
if([butt isEqualToString:labelbutt])
{
NSIndexPath *indexPath1 =[NSIndexPath indexPathForRow:j inSection:0];
NSLog(#"indexPath %#",indexPath1);
UITableViewCell *mycel=[self->table_AdviseTestDetails cellForRowAtIndexPath:indexPath1];
NSLog(#"cell %#",mycel);
UILabel *mybutton=(UILabel*)[mycel viewWithTag:100+j];
// mybutton.selected=YES;
NSLog(#"label %#",mybutton.text);
r=r+[mybutton.text floatValue];
NSLog(#"r %f",r);
}
}
}
range=1;
[table_AdviseTestDetails reloadData];
netcharges.text=0;
z1=0;
NSLog(#"label arr %#",LabelArr);
for(int i=0;i<LabelArr.count;i++)
{
NSString *y= [NSString stringWithFormat:#"%#",[[LabelArr objectAtIndex:i] valueForKey:#"value"]];
z1=z1+[y integerValue];
netcharges.text=[NSString stringWithFormat:#"%.2f",z1];
NSLog(#"net %#",netcharges.text);
//balance.text=[NSString stringWithFormat:#"%.2f",z];
}
netcharges.text=[NSString stringWithFormat:#"%.2f",[netcharges.text floatValue]-r];
NSLog(#"net %#",netcharges.text);
point.text=[NSString stringWithFormat:#"%.f",(floor([netcharges.text integerValue]/[[[Point_Result objectAtIndex:0]valueForKey:#"Value"] integerValue]))*[[[Point_Result objectAtIndex:0]valueForKey:#"Points"] integerValue]];
balance.text=netcharges.text;
NSLog(#"net %#",balance.text);
}
This is my code please ignore all code think is there [table_AdviseTestDetails reloadData];
when we reload table then my controller go in table datasource ...
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
and after this controller come below line of this
[table_AdviseTestDetails reloadData];
and after this controller goes in ...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
I have problem there .....I want when we reload table then first all datasource and delegate of UITableview run and then run other thing.....
So IN my above code ....I want fist reload table data use this
[table_AdviseTestDetails reloadData];
then run all delegate and datasource of table.....
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
and controller come after this below of [table_AdviseTestDetails reloadData];
this line .....how can do this ..please solve this problem ......thank in advance
these is the general sequence i am not including all the methods just required ones are here
#pragma mark
#pragma mark - Table view data source
1) -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
2) -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
3) -(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
4) -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
#pragma mark
#pragma mark - Table view delegate
1) -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
I have one UItableView divided in 2 sections.
How I can manage the method tableView didSelectRowAtIndexPath?
For the section 1 the method is running but for I don't know how to implement to the 2nd section.
Thanks
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
simply:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.section == 0) {
//Your code for Section 1 with the index 0
} else {
//Your code for Section 2 with the index 1
}
}
Just use the delegate method of UITableView :
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
And you can differentiate within section using the below code :
if (indexPath.section == 0)
if (indexPath.section == 1)
..upto n depends on number of sections in your TableView.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if(indexPath.section == 0){
if(indexpath.row == 0){
//Your code here
}
}
else if(indexPath.section == 0){
if(indexpath.row == 0){
//Your code here
}
}
}
I followed Simon Lee's tutorial on animating uitableview height change, and it's working amazing! The only problem I seem to be facing is that I can tap several cells, and they all will remain expanded regardless if another is already expanded. I only want one expanded at any given time, and if another is already expanded, for it to be closed before a new one expands.
Simon mentioned that we should "store a single NSIndexPath as the current selected item instead on an array of indexes." I'm not exactly sure how to do this. If anyone could help out that would be awesome!
what you have to do is just add the code below in the tableView delegate method tableView:didSelectRowAtIndexPath: below the line
[selectedIndexes setObject:selectedIndex forKey:indexPath];
code to add
NSArray *key = selectedIndexes.allKeys;
for (int i = 0; i < [key count]; i++) {
if (![[key objectAtIndex:i] isEqual:indexPath]) {
[selectedIndexes setObject:[NSNumber numberWithBool:NO] forKey:[key objectAtIndex:i]];
}
}
Happy Coding :)
Enjoy the Day!!
Yes you can colapse the cells using the delegate methods of UITableView -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath and - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath :-
First declare the variable to get theindexPath
NSIndexPath *selectedCellIndexPath;
then get the cell's indexPath in didSelectRowAtIndexPath :-
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
selectedCellIndexPath=indexPath;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationBottom];
}
Then do the collapsing and extending and set the lower and upper height:-
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(selectedCellIndexPath != nil
&& [selectedCellIndexPath compare:indexPath] == NSOrderedSame)
return yourCellsExtendedHeightInInteger;
return yourCellsCollapsedHeightInInteger;
}
or use
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(selectedCellIndexPath && indexPath.row == selectedCellIndexPath .row)
{
return 100;
}
return 44;
}
You can see this tutorial also:-
I have a UITableView put in edited mode.
self.tableView.editing = YES;
In this table view, I have some lines displayed in a custom cell, and I'd like to add one at the end that would allow the user to add an item (using another view).
So I wrote some lines :
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return "number of lines" + 1;
}
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.row+1 != [tableView numberOfRowsInSection:0]) {
return UITableViewCellEditingStyleDelete;
}
else {
return UITableViewCellEditingStyleInsert;
}
}
- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row + 1 == [tableView numberOfRowsInSection:0]) {
cell.textLabel.text = #"Add a line...";
}
else {
do the stuff in the custom cell
}
}
Doing this way, the UITableView allows to rearrange any line. I can move the first line after the "add line", and move the "add line" in first position.
How may I remove the arrange button on the "add line" cell and prevent other lines to go under this one ?
Or is there a better way to code this ?
Implement
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return ([indexPath row] != INDEX_OF_SPECIAL_ROW)
}
and
- (NSIndexPath *)tableView:(UITableView *)tableView
targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath
toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
if ([proposedDestinationIndexPath row] < NUMBER_OF_ROWS)
{
return proposedDestinationIndexPath;
}
NSIndexPath *otherPath = [NSIndexPath indexPathForRow:NUMBER_OF_ROWS-1 inSection:0];
return otherPath;
}
The much easier way would be to set a tableFooterView. You can place any UIView in there, so not only are you allowed to add a UITableViewCell but also a completely custom subclass. Doing it this way you will avoid all the unnecessary checks.
return UITableViewCellEditingStyleNone
For that row in - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
And check out – tableView:moveRowAtIndexPath:toIndexPath:
I would like to do some stuff when a user swipes to the right of a UITableViewCell, so I use
- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
However I tried to slide to the right and this didn't get invoked, why is this? All of my UITableView delegates are invoked.
I also have this in my code:
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
}
All I want to do is to add a subview when a swipe happens
Do you have a tableView:commitEditingStyle:forRowAtIndexPath: method?
To quote Apple:
Note: A swipe motion across a cell does not cause the display of a Delete button
unless the table view's data source implements the
tableView:commitEditingStyle:forRowAtIndexPath: method.
And, deleting that method in my project also causes the willBeginEditing not to be called.
// You missed this?
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)theTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleDelete;
}
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
return #"Remove";
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
}
}
Hope that helps.
Try adding this method to your tableView delegate methods:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source.
[self.listTableView beginUpdates];
...
I assume because your other delegate methods are being called that you included in your .h file #interface line? If using IB did you right click on the tableview and wire up delegate and dataSource?
Set the property of table view
tableView.editing = YES;