Use default blue background when UITableViewCell is selected - iphone

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;

Related

Loading Generic view from UITableViewController

Here is my problem :
I created a .xib with some label. I created a UITableViewController like this :
- (void)viewDidLoad {
[super viewDidLoad];
dataToShow = [[NSArray alloc] initWithObjects:#"cave",#"garage",nil];
}
- (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];
}
cell.textLabel.text = [dataToShow objectAtIndex:indexPath.row];
return cell;
}
The numberOfSectionInTableView and numberOfRowInSection are filled correctly, when running the programe the list shows as it should. I would like now, depending on which row the user click in the table view, to load the xib i created and fill the labels with text. Is that possible and if it is, how do I pull it off ? Thanks
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
yourNextViewController *nextViewXib = [[yourNextViewController alloc]initWithNibName:#"yourNextViewController" bundle:nil];
nextViewXib.yourLable.text=[dataToShow objectAtIndex:indexPath.row];
[self.navigationController pushViewController:nextViewXib animated:YES];
[nextViewXib release];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(#"selected row:%d",indexPath.row);
}
//in this mathod you get will row is calling..

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.

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

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

Multi Select Table View Cell and no Selection Style

I have a basic UITableView that I want to enable the Mail.app style check marks while having no selection style. I have the following snippet:
#define UITableViewCellEditingStyleMultiSelect (3)
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleMultiSelect;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
However, this will never display the check marks on selection (although the empty circles are displayed). Any ideas on how to fix it? I know it uses undocumented features but I'd really like to add support for the check marks. My actual example uses a very customized UITableViewCell and I can't enable the selection style!
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
if (self.tableView.isEditing) {
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
} else {
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
return cell;
}
-(UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleMultiSelect;
}
-(IBAction) switchEditing {
[self.tableView setEditing:![self.tableView isEditing]];
[self.tableView reloadData]; // force reload to reset selection style
}