Custom accessory in uitableview doesn't call accessorybuttontapped delegate - ios

I'm having a custom cell that I create like this:
CategoryCellTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:#"poiCell"];
if (!cell)
{
cell = [[CategoryCellTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"poiCell"];
}
// Get current POI
Rank * poi = [_data objectAtIndex:indexPath.row];
cell.text.text = poi.name;
cell.categoryImageView.image = [UIImage imageNamed:#"animals.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"map"]];
cell.accessoryView = imageView;
return cell;
The accessoryView shows up. But when I click on it, it doesn't come in my:
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"Accessory clicked");
}
Do I need to set some delegate right? I thought this was already implemented for you so you could always use it?

You need your own handler for you custom view. This delegate method called only for default accessory button.
For example you may add custom UIButton with image as accessory view and add target and action to this button.

Related

Changing the accessory view of cell in a user defined function

I want to change the accessory view of the cell. When the user clicks on the right navigation bar item an action occurs, in that action i want to change the accessory view. how can i do this, when i used this code its asking for NSindexPath thing so i have no which value i can give here , in order to change the view of all cells.
MyCustomCell *cell = (MyCustomCell *) [self.tableView cellForRowAtIndexPath:helloIndexPath];
if( cell.accessoryType == UITableViewCellAccessoryDisclosureIndicator)
{
cell.accessoryView = [[UIImageView alloc]initwithImage:[UIImage imageNamed:#"hello.png"]];
}
I guess you should put condition in table view - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
Here you can put a if-condition.
if(flagForAction){
cell.accessoryView = [[UIImageView alloc]initwithImage:[UIImage imageNamed:#"hello.png"]];
}
else
{
cell.accessoryView = [[UIImageView alloc]initwithImage:[UIImage imageNamed:#"<no-Hello>.png"]];
}
All you need to do is set your flag and reload table at action. by calling [<your table> reloadData];

Improper functioning tableView data and recharging

-Hi, I have a problem with a tableview reloading data and what I want to do is that you put the selected cell in blue and change an image that is, the problem is that if I make the [self.mytableview reloadData] in the didselectedRow blue background disappears and if I do the image of the cell does not change, I'm a bit lost with this piece of code I give thanks
if (indexPath.row == _selectedRow) {
UIImageView *favView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"favIconSelected.png"]];
CGRect frame = favView.frame;
frame.origin.x = 294;
frame.origin.y = 7;
favView.frame = frame;
[cell.contentView addSubview:favView];
[favView release];
}
cell.selectedBackgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:SELCETED_BGIMGCELL]]autorelease];
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Product *p = [_productList objectAtIndex:indexPath.row];
_selectedRow=indexPath.row;
[_delegate productWasSelected:p];
[self.myTableView reloadData];
}
You don't need to call reloadData at all. All you need to do is to update the cell that was selected.
In the didSelectRowAtIndexPath method you can get the cell directly by calling cellForRowAtIndexPath on the tableView. This will return the actual cell that's on display and you can directly set the image it's displaying.
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.imageView.image = ...;
Alternatively you could control the image display from your cell subclass if you provide it with both the normal and selected images when you configure each instance.

How do I center an image in a UITableViewCell?

I use this code to create an image inside a cell:
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"CellIdentifier"] autorelease];
switch (indexPath.row) {
...
case 5:
cell.imageView.image = [UIImage imageNamed:#"Search.png"];
break;
default:
break;
}
return cell;
}
How do I center the image in the cell? Do I HAVE to subclass UITableViewCell?
You can do this if you don't use the provided cell.imageView but instead add a new UIImageView to the cell. Try something like this at the appropriate place in cellForRowAtIndexPath:
UIImageView *newImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Search.png"]];
newImageView.center = cell.center;
[cell addSubview:newImageView];
Better and efficient way to do this is using Custom UITableViewCell. So in feature, you can easily customize the TableViewCell.
You can do this through both code and Interface builder.
Custom UITableViewCell Using Interface Builder
Customize Table View Cells for UITableView
cell.imageView.center = cell.center;

UITableViewCell show selected when switching Views

i got a UITableViewCell which i want to stay selected when clicked on it. So i have this code which works just fine:
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
cell.textLabel.text = [NSString stringWithFormat:#"%#", [[[self.dataSource objectAtIndex:indexPath.row] lastPathComponent] stringByDeletingPathExtension]];
//cell.selectionStyle = UITableViewCellSelectionStyleNone;
UIView *bgColorView = [[UIView alloc] init];
[bgColorView setBackgroundColor:[UIColor brownColor]];
[cell setSelectedBackgroundView:bgColorView];
return cell;
}
Everytime i click on a cell it gets selected with the brown color and stays selected until i click another cell... perfect
But when i dismiss the view by clicking on the back button of my navigationcontroller and then i switch back to my view my cell is not selected anymore. So how can i achieve that the cell which was selected before i switched the views still is selected when i come back to the view ?
I thought i maybe have to create a property from the tableview and then select the row in the viewDidLoad again. The selectedRow index i could save in the nsuserdefaults.. but i hope there is a simpler solution.
In your -viewDidLoad method, add this to the bottom: self.clearsSelectionOnViewWillAppear = NO; Which would make it look something like this:
- (void)viewDidLoad {
[super viewDidLoad];
self.clearsSelectionOnViewWillAppear = NO;
}

Grouped UITableView - Replicate the Settings app table view

I want to create a view like this in my iPhone App:
I do not know exactly what is this control in iOS, that maybe I can set an icon and text in the left side and that small sign in the right side.
I have implemented a TableView, there I was able to set these stuff, like this:
[[cell textLabel] setText:customer.name];
[[cell textLabel] setTextColor:[UIColor blackColor]];
[[cell imageView] setImage:[UIImage imageNamed:#"icon.png"]];
//[[cell detailTextLabel] setText:#"Awsome weather idag"];
cell.accessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
But how can I make it works like that view in the picture?
It is pretty simple, follow the steps below and in case of doubts check out the UITableView documentation:
1. Create a grouped table view:
Programmatically:
CGRect tableFrame = CGRectMake(0, 0, 200, 200);
UITableView *tableView = [[UITableView alloc] initWithFrame:tableFrame style:UITableViewGroupedStyle];
tableView.delegate = self;
tableView.dataSource = self;
[self.view addSubview:tableView];
Allocating a UITableViewController subclass (common case):
MyTableViewController *controller [[MyTableViewController alloc] initWithStyle: UITableViewGroupedStyle];
[self.navigationController pushViewController:controller animated:NO];
Through Interface Build:
Expand the Utilities Menu (top right corner icon);
Select your table view (click on it);
Click on the attributes inspector (top right corner fourth icon);
Under Table View, click on the style dropdown and select grouped.
2. Implement the UITableViewDataSource protocol:
Basically add this three functions to your controller.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in a given section.
return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Configure the cells.
return cell;
}
3. Configuring the cells:
The default style of a UITableViewCell has an image view (UIImageView), a title label (UILabel) and an accessory view (UIView). All you need to replicate the table view in the image you provided.
So, you're looking for something like this in tableView:cellForRowAtIndexPath::
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString * const cellIdentifierDefault = #"default";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifierAccount];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifierAccount];
}
if (indexPath.section == 0) {
cell.imageView.image = [UIImage imageName:#"bluetooth_icon"];
cell.textLabel.text = #"Bluetooth";
// Additional setup explained later.
}else{
if (indexPath.row == 0) {
cell.imageView.image = [UIImage imageName:#"general_icon"];
cell.textLabel.text = #"General";
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}else{
cell.imageView.image = [UIImage imageName:#"privacy_icon"];
cell.textLabel.text = #"Privacy";
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
}
return cell;
}
The property accessoryType defines what is going to appear on the right side of a cell, a list of accessory types can be found here.
In the first cell (bluetooth), you'll need to create a custom accessory view and assign it to the cell's accessoryView property. A very naive example of how to achieve this is given below:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString * const cellIdentifierDefault = #"default";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifierAccount];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifierAccount];
label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
label.textAlignment = NSTextAlignmentRight;
cell.accessoryView = label;
}else{
label = (UILabel *) cell.accessoryView;
}
cell.imageView.image = [UIImage imageName:#"bluetooth_icon"];
cell.textLabel.text = #"Bluetooth";
label.text = #"Off";
return cell;
}
Hope this helps, Mateus
For fast output, you can use some library like
https://github.com/escoz/QuickDialog
protip, in my experience, solutions like this leaves you more tangled when Changes come in.
Some times you only want to change one specific label on once specific view, thats not gona be easy in any ready-made solution.
Look into UITableView Sections. That is what separates the groups apart.

Resources