How to detect a swipe-to-delete gesture in a customized UITableviewCell? - iphone

I have customized a UITableViewCell and I want to implement "swipe to delete". But I don't want the default delete button. Instead, I want to do something different. What would be the easiest way to implement this? Are there some methods which get called when the user swipes to delete a cell? Can I prevent then the default delete button from appearing?
Right now I think I must implement my own logic to avoid the default delete button and shrink animations which happen in swipe to delete in the default implementation of UITableViewCell.
Maybe I have to use a UIGestureRecognizer?

If you want to do something completely different, add a UISwipeGestureRecognizer to each tableview cell.
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell.
UISwipeGestureRecognizer* sgr = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(cellSwiped:)];
[sgr setDirection:UISwipeGestureRecognizerDirectionRight];
[cell addGestureRecognizer:sgr];
[sgr release];
cell.textLabel.text = [NSString stringWithFormat:#"Cell %d", indexPath.row];
// ...
return cell;
}
- (void)cellSwiped:(UIGestureRecognizer *)gestureRecognizer {
if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
UITableViewCell *cell = (UITableViewCell *)gestureRecognizer.view;
NSIndexPath* indexPath = [self.tableView indexPathForCell:cell];
//..
}
}

Here are two methods that can be used to avoid the Delete Button:
- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
and
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

Related

UITableViewCell background view/image

Where do I set backgroundImage of tableview cell in cellForRowAt.. And how do I do that ? This is how I did it and it kinda works, but I want to know is that right method for setting backgroundimage (cell.backgroundView) and is it right place inside if(cell==nil) or outside that "if".?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"RootViewCellIdentifier";
UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"image1.png"]];
}
return cell;
}
Used to do that, but nowadays I prefer willDisplayCell:
- (void)tableView:(UITableView *)tableView
willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.backgroundColor = [UIColor redColor];
}
Documentation says
A table view sends this message to its delegate just before it uses cell to draw a row, thereby permitting the delegate to customize the cell object before it is displayed. This method gives the delegate a chance to override state-based properties set earlier by the table view, such as selection and background color. After the delegate returns, the table view sets only the alpha and frame properties, and then only when animating rows as they slide in or out.
When using cellForRowAtIndexPath, there were some weird special cases, where cell customization just didn't work fully as I wanted. No such problems with willDisplayCell.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
[cell.imageview setImage:[UIImage imageNamed:#"image1.png"]];
}
return cell;
}
try with this might be helpful...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"RootViewCellIdentifier";
UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
UIImageView *backimg = [[UIImageView alloc] init];
backimg.frame = CGRectMake(0, 0, 320, 44);
backimg.tag = 20;
[cell.contentView addSubview:backimg];
}
UIImageView *imag = (UIImageView *)[cell.contentView viewWithTag:20];
[imag setImage:[UIImage imageNamed:#"sky.png"]];
return cell;
}
The best place to set a background view for a cell is the tableView:willDisplayCell:forRowAtIndexPath: method.
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"image1.png"]];
}

Changing the image of the cell when it's checked

In my UITableView, i need to get the event of the user, so when the cell is selected(checked), i put btn_checked.png and when it's unchecked, i put the btn_unchecked.png.
I guess, what i try to do should be in the didSelectRowAtIndexPath method, so this is my code:
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSArray *listData =[self->tableContents objectForKey:
[self->sortedKeys objectAtIndex:[indexPath section]]];
UITableViewCell* theCell = [tableView cellForRowAtIndexPath:indexPath];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"btn_unchecked.png"]];
theCell.accessoryView = imageView;
}
My snippet code doesn't work, so the btw_unchecked.png isn't placed when the row is checked, also, i need to get the event of checked/unchecked to place the right image to the right event.
Thanx in advance.
As an example, this code snippet will toggle the cell's checkmark when the cell is tapped. The pattern is to force a selective reload from the table's datasource when the delegate method is invoked.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Dequeue the cell... then:
static NSString *CellIdentifier = #"tvcOEList";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.accessoryType = (cell.accessoryType == UITableViewCellAccessoryNone) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
}
You can use following code.
[cell.contentView addSubview:imageView];
i hope this will help you..

How to create a table view with only one row selectable

Hi need to create a table view with only one cell selectable like this img.
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:#"CellWithSwitch"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"CellWithSwitch"] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.font = [UIFont systemFontOfSize:14];
if(indexPath.row == 0)
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
cell.textLabel.text = #"Sound Effects";
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)path {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:path];
if(path.row == 0)
{
if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
cell.accessoryType = UITableViewCellAccessoryNone;
} else {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
}
}
if you like only one row selectable you have to manage this in the methods of the table:
in - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
you need to implement the selection logic. if you click on a non-selected object, you select it and deselect all others, or only the one that is currently selected (if there is one selected). If you click on a selected object, just deselect it here
dont forget to do [theTableView reloadData];
in case your screen does not update if you change something in the table's content
then to redraw you just ensure that your method - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
ensures that the object which is selected is marked somehow. This way you can also do nice things like showing a toggle ON/OFF button or anything you may whish.

Use default blue background when UITableViewCell is selected

For a better user experience, I would like my UITableViewCell to have the default blue styling when a user taps it. Currently, there is no styling at all. Shouldn't the following this be all I need?
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView selectRowAtIndexPath:indexPath
animated:NO scrollPosition:UITableViewScrollPositionNone];
// [The rest of my code here that loads up the detail view]
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// [bunch of code]
UITableViewCell *cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:PlaceholderCellIdentifier] autorelease];
// [bunch of code that sets the text of the cell];
return cell;
}
Go to cellForRowAtIndexPath and use
cell.selectionStyle= UITableViewCellSelectionStyleBlue;

How to get cell text based on indexPath?

I have a UITabBarController with more than 5 UITabBarItems so the moreNavigationController is available.
In my UITabBarController Delegate I do the following:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
//do some stuff
//...
UITableView *moreView = (UITableView *)self.tabBarController.moreNavigationController.topViewController.view;
moreView.delegate = self;
}
I want to implement a UITableViewDelegate so I can capture the row that was selected, set a custom view property and then push the view controller:
- (void)tableView:(UITableView *)tblView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//how can I get the text of the cell here?
}
I need to get the text of a cell when the user taps on a row. How can I accomplish this?
- (void)tableView:(UITableView *)tblView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//how can I get the text of the cell here?
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *str = cell.textLabel.text;
}
A better Solution is to maintain Array of cell and use it directly here
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
Service *service = [self.nearMeArray objectAtIndex:indexPath.row];
cell.textLabel.text = service.name;
cell.detailTextLabel.text = service.description;
if(![self.mutArray containsObject:cell])
[self.mutArray insertObject:cell atIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [self.mutArray objectAtIndex:indexPath.row];
NSString *str = cell.textLabel.text;
}
Swift 5
You can always get your cell from UITableVIew or UICollectionView based on IndexPath like this:
tableView.cellForItem(at: indexPath)
collectionView.cellForItem(at: indexPath)