How to activate tableView didSelectRowAtIndexPath in 2 sections? - iphone

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

Related

Change dynamically insertion/deletion control in UITableview row objective c?

I've tried with following code. It works fine. But I can not figured out how to achieve few things.
How to add insertion control when I clicked "Delete" button instead of deletion control on the same row?
If I moved "section 3" in between "Section 1" and "Section 2", how to update all the three sections with deletion control in them?
So far I've got following output using below code.
-(void)tableView:(UITableView *)aTableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
//Handle things when deletion control clicked!
//[sectionArray removeObjectAtIndex:indexPath.row];
//[self.tableView reloadData];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
//Handle things when insertion control clicked!
}
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
// No editing style if not editing or the index path is nil.
if (isEditMode == NO || !indexPath) return UITableViewCellEditingStyleNone;
if (indexPath.row == 1){
return UITableViewCellEditingStyleDelete;
}else if (indexPath.row == 2){
return UITableViewCellEditingStyleInsert;
}else if (indexPath.row == 3){
return UITableViewCellEditingStyleInsert;
}else {
return UITableViewCellEditingStyleDelete;
}
return UITableViewCellEditingStyleNone;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath
toIndexPath:(NSIndexPath *)toIndexPath {
NSString *item = [sectionArray objectAtIndex:fromIndexPath.row];
[sectionArray removeObject:item];
[sectionArray insertObject:item atIndex:toIndexPath.row];
NSLog(#"%#", sectionArray);
}
Solution for your second question is that you need to refresh your table view once you performed your action such as deletion and insertion
I hope this links also helpful for you.
http://code4app.net/ios/UITableView-actions/50bf05986803fa8e5c000001
http://www.appcoda.com/model-view-controller-delete-table-row-from-uitableview/
For any time of UI problem you also got demo applications from this side.
http://code4app.net

how to find section in didSelectRowAtIndexPath: method

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

how to find which UITableView row has been clicked ? I have multiple UITableviews on UIScrollView.

I have implemented around 10 tables on UIScrollView. I want to know which row of which UITable has been clicked.
thanks in advance.
You can add tags to each of the table and then check it in didSelectRowAtIndexPath
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if(tableView.tag == 1){
//do something
}
else if(tableView.tag == 2){
//do something else
}
// and so on
}
Or else, you could check which instance of tableview is calling the delegate method:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if(tableView == self.tableView1){
//do something
}
else if(tableView == self.tableView2){
//do something else
}
// and so on
}

iPhone - How add a final line in a UITableView to add items, preventing rearranging of this line

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:

add + button before a row

How can i add green + button before a row in group table see in image
- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.row == 1)//type your condition here
{
return UITableViewCellEditingStyleDelete;
}
else{
return UITableViewCellEditingStyleInsert;
}
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
//type your code here
}else if(editingStyle == UITableViewCellEditingStyleInsert){
//type your code here
}
}
You can refer to this sample code link
http://developer.apple.com/library/ios/#samplecode/ListAdder/Introduction/Intro.html#//apple_ref/doc/uid/DTS40010275