add + button before a row - iphone

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

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 activate tableView didSelectRowAtIndexPath in 2 sections?

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

Add "Swipe to delete" but not in all cells

I want to add in my tableview the possibility to "swipe to delete", but I don't want this for the last cell of the table!
I can check in - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath if the indexPath.row is the last one, but what I want is that if the user swipes on the last cell, nothing will appear (while in the others cells appears the text "delete").
I've tried this
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row != ([array count]-1)) {
return #"delete";
}
else {
NSString *a;
return a;
}
}
but of course it doesn't works (the app crashes).
I've tried with
return #"";
but the red button appears (with no text)!
What do you suggest me?
Thanks!
Try
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([indexPath row] == [tableView numberOfRowsInSection:[indexPath section]] - 1)
{
return UITableViewCellEditingStyleNone;
}
return UITableViewCellEditingStyleDelete;
}
The app crashes since you return an uninitialized pointer. But even then, you're doing it wrong ;-)
You want to implement tableView:editingStyleForRowAtIndexPath: and return UITableViewCellEditingStyleDelete for all cells, except for the last. You need to return UITableViewCellEditingStyleNone for the last cell to prevent that it can be deleted.

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:

issue with swiping a UITableViewCell

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;