EditActionsForRowAtIndexPath not being called - objective-c

I want to make 2 buttons in tableView cell swipe and i override the method
editactionsforrowatindexpath
and it simple not work. With iOS 9.
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
}
-(NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewRowAction *button = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:#"Delete" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
{
// Logic
}];
button.backgroundColor = [UIColor redColor]; //arbitrary color
UITableViewRowAction *button2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:#"Send SMS " handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
{
// Logic
}];
button2.backgroundColor = [UIColor lightGrayColor];
return #[button, button2];
}

Found the problem,
the problem was the the DataSource Delegate and the TableViewDelegate was at 2 diffrent classes

Related

UITableViewCell on left swipe doesn't trigger editActionsForRowAtIndexPath

I have a scrollview which contains different subviews, one of which is tableview. I also have a UITapGestureRecognizer and UISwipeGestureRecognizer initialized on the scroll view. The tableview uses a nib to initialize its cell.
What I would like to do is that, when I swipe the cell to the left, I want a button to swipe in. I am able to trigger my handler when left swipe the cell. The problem is that it doesn't trigger the editActionsForRowAtIndexPath where I am handling the code to create a button.
Instead, it goes to editingStyleForRowAtIndexPath and ends executing showing a red icon with a minus sign to the left of the cell.
My swipe handler and related code is as below:
- (void)swipeLeftDirection:(UIGestureRecognizer*)gesture
{
if (!tblInviteesTable.editing)
{
CGPoint location = [gesture locationInView: tblInviteesTable];
iPath = [tblInviteesTable indexPathForRowAtPoint:location];
// quitEditing = NO;
OpenHouseTableViewCell *currentCell = (OpenHouseTableViewCell *)[self tableView:tblInviteesTable cellForRowAtIndexPath:iPath];
[currentCell setEditing:YES];
[tblInviteesTable setEditing:YES animated:YES];
[self tableView:tblInviteesTable editActionsForRowAtIndexPath:iPath];
}}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSLog(#"Handled editing style! %ld", (long)editingStyle);
} else {
NSLog(#"Unhandled editing style! %ld", (long)editingStyle);
}
}
-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
if ([tableView isEqual: tblInviteesTable]){
UITableViewRowAction *button = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:#"Resend" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(#"Resending email");
// [_delegate listingAgentsView: self selected: indexPath];
}];
button.backgroundColor = [UIColor redColor];
return #[button];
}
return #[];
}
Any help is greatly appreciated.

Objective-C : Always display 3 rows in UITableView

After scrolling i want my TableView to display like this :
not like this :
and i have set my rowHeight = tableViewHeight / 3:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return CGRectGetHeight(self.tableView.frame)/3;
}
There are a couple things you need for this. Firstly, you need to detect when the table view has stopped scrolling:
How to know exactly when a UIScrollView's scrolling has stopped?
Next, you need to set the scroll view position:
How can I set the scrolling position of an UIScrollView?
The position you're going to want to set it might be the tableview contentOffset - (contentOffset % rowHeight) to move it up to show the row partially shown at the top.
Follow this steps:
In .h , declare
NSMutableArray *arrayForBool;
NSMutableArray *arrPastOrder;
In .m ,
- (void)viewDidLoad {
arrPastOrder=[[NSMutableArray alloc] init];
int range=1+arc4random()%10;
for(int i=0;i<range;i++)
{
NSMutableArray *arrinside=[[NSMutableArray alloc] init];
for(int j=0;j<3;j++)
{
if(j==0)
[arrinside addObject:[NSString stringWithFormat:#"Some Header %i",i]];
else
[arrinside addObject:[NSString stringWithFormat:#"Subindex %i",j]];
}
[arrPastOrder addObject:arrinside];
}
arrayForBool=[[NSMutableArray alloc]init];
for (int i=0; i<[arrPastOrder count]; i++)
{
[arrayForBool addObject:[NSNumber numberWithBool:YES]];
}
}
#pragma mark -
#pragma mark TableView DataSource and Delegate Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if ([[arrayForBool objectAtIndex:section] boolValue])
{
return [[arrPastOrder objectAtIndex:section] count]-1;
}
else
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *cellid=#"hello";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellid];
if (cell==nil)
{
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellid];
}
BOOL manyCells = [[arrayForBool objectAtIndex:indexPath.section] boolValue];
/********** If the section supposed to be closed *******************/
if(!manyCells)
{
cell.backgroundColor=[UIColor clearColor];
NSLog(#"cellForRowAtIndexPath---if");
cell.textLabel.text=#"";
}
/********** If the section supposed to be Opened *******************/
else
{
//cell.textLabel.text=[NSString stringWithFormat:#"%# %ld",[sectionTitleArray objectAtIndex:indexPath.section],indexPath.row+1];
// ic_keyboard_arrow_up_48pt_2x ic_keyboard_arrow_down_48pt_2x.png
cell.textLabel.text=[NSString stringWithFormat:#"%#",[[arrPastOrder objectAtIndex:indexPath.section] objectAtIndex:indexPath.row+1]];
}
cell.textLabel.textColor=[UIColor blackColor];
return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [arrPastOrder count];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"%li--%li",(long)indexPath.section, (long)indexPath.row);
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([[arrayForBool objectAtIndex:indexPath.section] boolValue]) {
return 40;
}
return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 100;
}
#pragma mark - Creating View for TableView Section
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *sectionView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 540,100)];
sectionView.backgroundColor=[UIColor whiteColor];
sectionView.tag=section;
UILabel *viewLabel=[[UILabel alloc]initWithFrame:CGRectMake(10, 30, 530, 40)];
viewLabel.backgroundColor=[UIColor whiteColor];
viewLabel.textColor=[UIColor blackColor];
viewLabel.font=[UIFont boldSystemFontOfSize:20];
viewLabel.text=[NSString stringWithFormat:#"%#",[[arrPastOrder objectAtIndex:section] objectAtIndex:0]];
[sectionView addSubview:viewLabel];
/********** Add UITapGestureRecognizer to SectionView **************/
UITapGestureRecognizer *headerTapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(sectionHeaderTapped:)];
[sectionView addGestureRecognizer:headerTapped];
return sectionView;
}
#pragma mark - Table header gesture tapped
- (void)sectionHeaderTapped:(UITapGestureRecognizer *)gestureRecognizer{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:gestureRecognizer.view.tag];
//Get which section is open, from here Set the value of arrafool. and expand collapse. Normally whole table is expanded.
}
Just run it. You will get the same structure. happy Coding !!

updateSwitchAtIndexPath crashes

I', have a UITableView with UISwitch in some cells.
I want to capture events, but it crashes when I try to add the indexPath.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//Create cell with all data
UISwitch *switchview = [[UISwitch alloc] initWithFrame:CGRectZero];
cell.accessoryView = switchview;
[switchview addTarget:self action:#selector(updateSwitchAtIndexPath) forControlEvents:UIControlEventTouchUpInside];
}
- (void)updateSwitchAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
UISwitch *switchView = (UISwitch *)cell.accessoryView;
if ([switchView isOn]) {
NSLog(#"ON");
} else {
NSLog(#"OFF");
}
}
It crashes, I think because I'm not adding the indexPath parameter, but I can't get how to set it.
-[ParkingData updateSwitchAtIndexPath]: unrecognized selector sent to instance 0x7b88eb0
Thanks!
UPDATE
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//Create cell with all data
UISwitch *switchview = [[UISwitch alloc] initWithFrame:CGRectZero];
cell.accessoryView = switchview;
[switchview addTarget:self action:#selector(updateSwitchAtIndexPath:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)updateSwitchAtIndexPath:(UISwitch *)switchview {
if ([switchView isOn]) {
NSLog(#"ON");
} else {
NSLog(#"OFF");
}
}
[switchview addTarget:self action:#selector(updateSwitchAtIndexPath) forControlEvents:UIControlEventTouchUpInside];
should be
[switchview addTarget:self action:#selector(updateSwitchAtIndexPath:) forControlEvents:UIControlEventTouchUpInside];
you are missing just a colon. because your method has parameter.
- (void)updateSwitchAtIndexPath:(NSIndexPath *)indexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UISwitch *switchview = [[UISwitch alloc] initWithFrame:CGRectZero];
cell.accessoryView = switchview;
[switchview addTarget:self action:#selector(updateSwitchAtIndexPath:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)updateSwitchAtIndexPath:(id)sender
{
// see the line BELOW
NSIndexPath *indexPath = [[(UITableView*)[sender superview] superview]indexPathForCell: (UITableViewCell*)[sender superview]];
if ([sender isOn])
NSLog(#"ON");
else
NSLog(#"OFF");
}
In the above code sender means UISwitch and if u want indexPath of cell then the marked line helps to you.

iOS uitableview cell only gets selected for a moment

In my code i want to select a cell in a UITableView. I perform the selection here, but every time it should be selected it gets selected only for a moment and then it loses its selection.
What did I do wrong?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"SimpleTableIdentifier"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"SimpleTableIdentifier"];
}
cell.textLabel.text = [[self tableViewArray] objectAtIndex:indexPath.row];
if(indexPath.row == self.selectedTopicIndex)
{
NSLog(#"DIE DIE DIE DIE DIE!!!");
[cell setSelected:YES];
[cell setSelectionStyle:UITableViewCellSelectionStyleBlue];
[cell setSelected:YES];
}
return cell;
}
Just move your selection logic inside -tableView:willDisplayCell:forRowAtIndexPath: method.
Something like this:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.row == self.selectedTopicIndex)
{
NSLog(#"DIE DIE DIE DIE DIE!!!");
[cell setSelectionStyle:UITableViewCellSelectionStyleBlue];
[cell setSelected:YES];
}
}

UITableView and Selecting Items

I am currently creating a simple grouped UITableView that when the user selects for example "Apple" I would like a image I have in my project to be loaded into a Image View of a Apple.
I have seen some examples on the internet but I am trying to see what else is out there.
Any suggestions would be greatly appreciated. Thanks.
Either set the cell.imageView.highlightedImage, or in the didSelectRowAtIndexPath method, change the cell.imageView.image from nil to some [UIImage imageNamed:#"myImage.png"]:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *kCustomCellID = #"com.me.foo.cell";
UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCustomCellID] autorelease];
cell.textLabel.highlightedTextColor = [UIColor blueColor];
cell.imageView.image = nil;
cell.imageView.highlightedImage = [UIImage imageNamed:#"myImage.png"];
return cell;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(iPadRootViewControllerTableCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Do this if you didnt set the highlightedImage in cellForRowAtIndexPath
// [self tableView:tableView cellForRowAtIndexPath:indexPath].imageView.image = [UIImage imageNamed:#"myApple.png"];
}