I am having custom-cell in which i put the UIButton as the CheckBox.
I want the array of selected checkBox indexPath in ViewController.
I am trying to set the UIButton Action event into the ViewController but it is not accessible. Here is my Code,
-(UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomTableViewCellSaveSearchTableViewCell *cell = [tableView1 dequeueReusableCellWithIdentifier:#"SaveSearchCell"];
if(!cell)
{
cell = [[[NSBundle mainBundle]loadNibNamed:#"CustomTableViewCellSaveSearchTableViewCell" owner:self options:nil]objectAtIndex:0];
}
[self setText:cell];
[cell.btnDelete addTarget:self action:#selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
cell.btnDelete.tag = indexPath.row;
return cell;
}
-(void)btnClick : (UIButton *)btn
{
// I Can't accaes this method.
}
How can i get this.
UITableViewCells have a state of their own to reflect if they are selected or not. You don't need a button in the cells (it is discouraged for UX reasons anyway). Check here for a tutorial.
Do according to these three floowing method.
- (void)viewDidLoad {
[super viewDidLoad];
chekMarkArray = [[NSMutableArray alloc]init];
int totalData = 10;
chekMarkArray=[[NSMutableArray alloc] initWithCapacity:totalData];
for (int i=0; i<totalData; i++) {
[chekMarkArray addObject:#(0)];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UIButton *testButton;
if (cell == nil ||1) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
testButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
testButton.frame = CGRectMake(100, 5,25, 25);
testButton.tag = indexPath.row;
BOOL selectindex = [[chekMarkArray objectAtIndex:indexPath.row] boolValue];
if (!selectindex) {
testButton.backgroundColor = [UIColor greenColor];
}else{
testButton.backgroundColor = [UIColor redColor];
}
[testButton addTarget:self action:#selector(DropDownPressed:) forControlEvents:UIControlEventTouchDown];
[cell addSubview:testButton];
}
return cell;
}
checked button action
-(void)btnClick : (UIButton *)btn
{
UIButton *button = (UIButton*)sender;
BOOL currentStatus = [[chekMarkArray objectAtIndex:button.tag] boolValue];
//deselect all selection
for (int i=0;i<chekMarkArray.count;i++) {
[chekMarkArray replaceObjectAtIndex:i withObject:#(0)];//comment this line for multiple selected button
}
//select current button
[chekMarkArray replaceObjectAtIndex:button.tag withObject:#(!currentStatus)];
[self.tableView reloadData];
}
You may use this code instead of button
first Alloc NsmutableArray in ViewDidLoad
and put an Button outside Tableview to complete your action.
then add
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if(cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[selectedRowsArray addObject:[arrShareTickets objectAtIndex:indexPath.row]];
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
[selectedRowsArray removeObject:indexPath];
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
Related
I added button to table view cell, i want to select one button out of all when user tap on cell
- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"cell";
UITableViewCell *cell = [theTableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
_radioButton = [UIButton buttonWithType:UIButtonTypeCustom];
_radioButton.frame = CGRectMake(30, 0, 30, 30);
[_radioButton setImage:[UIImage imageNamed:#"radio_empty.png"] forState:UIControlStateNormal];
[_radioButton setImage:[UIImage imageNamed:#"radio.png"] forState:UIControlStateSelected];
cell.accessoryView = _radioButton;
_radioButton.tag = indexPath.row;
}
if ([indexPath isEqual:selectedIndex])
{
_radioButton.selected = YES;
}
else
{
_radioButton.selected = NO;
}
cell.textLabel.text = [self.Option objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)theTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
selectedIndex = indexPath;
[theTableView reloadData];
//I tried this also but not working
if (_radioButton.tag == indexPath.row) {
[_radioButton setImage:[UIImage imageNamed:#"radio.png"] forState:UIControlStateSelected];
}
}
I tried a lot. But it's not working.
I want to show selected image when user tap on cell. Either if we use button or image view
I got the solution like this.
NSString *selectedIndex;
- (void)tableView:(UITableView *)theTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[theTableView deselectRowAtIndexPath:indexPath animated:NO];
if(indexPath.row != [selectedIndex intValue]) {
// reset the active account
selectedIndex = [#(indexPath.row) stringValue];
// tell the table to rebuild itself
[theTableView reloadData];
}
}
- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"cell";
UITableViewCell *cell = [theTableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
if(indexPath.row == [selectedIndex intValue]) {
cell.imageView.image = [UIImage imageNamed:#"radio.png"];
}
else {
cell.imageView.image = [UIImage imageNamed:#"radio_empty.png"];
}
cell.textLabel.text = [self.Option objectAtIndex:indexPath.row];
return cell;
}
I have a table with number of cells, with each cell having one UIButton. When I click on that button the button cell background color should change. Here is my partial code for your understanding.
When I click on this button all my tableview cells background colors are getting changed. How to do it only for the selected cell?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = #"SimpleTableCell";
cell = (MenuCell *)[menuTableview dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"MenuViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
[cell initState];
// Listens for method calls on cell
cell.delegate = self;
}
if ((self.TempArray.count != 0)) {
for (int i=0; i<TempArray.count; i++) {
if ([self.TableviewArray objectAtIndex:indexPath.row] == TempArray[i]) {
if (cell.backgroundColor != [UIColor lightGrayColor]) {
cell.backgroundColor = [UIColor lightGrayColor];
}
UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
button.tag=indexPath.row;
[button addTarget:self action:#selector(aMethod:) forControlEvents:UIControlEventTouchDown];
[button setTitle:#"UnHide" forState:UIControlStateNormal];
button.frame = CGRectMake(200.0, 20.0, 160.0, 40.0);
[cell.contentView addSubview:button];
}
}
}
// Set index for this cell (it could be wrong if cell is re-used)
cell.downloadIndex = (int)indexPath.row;
Download *download = [TableviewArray objectAtIndex:indexPath.row];
cell.downloading = download.downloading;
cell.completed = download.completed;
cell.MenuLang.text = download.name;
cell.downLoadPrgView.progress = download.percentageDownloaded;
// Check for completed status
cell.completed = download.completed;
cell.CompletedMenuLang.text = download.name;
return cell;
}
-(void)aMethod:(UIButton*)sender {
NSLog(#"I Clicked a button %d",sender.tag);
int row = sender.tag;
}
Just implement below code, you can change background colour of particular cell as well as hide the selected button.
-(void)aMethod:(UIButton*)sender {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:sender.tag inSection:0];
Friends1TableViewCell *cell = (Friends1TableViewCell *)[self.tblVW cellForRowAtIndexPath:indexPath];
cell.backgroundColor = [UIColor redColor];
sender.hidden = YES;
}
-(IBAction)BTNTapped:(UIButton *)sender
{
CGPoint buttonPosition = [sender convertPoint:CGPointZero
toView: menuTableview];
NSIndexPath *tappedIP = [menuTableview indexPathForRowAtPoint:buttonPosition];
}
do change using this indexpath of cell
Here is my answer..
-(void)aMethod:(UIButton*)sender
{
NSLog(#"I Clicked a button %d",sender.tag);
int row = sender.tag;
NSIndexPath *path = [NSIndexPath indexPathForRow:row inSection:0];
UITableViewCell *cell = [menuTableview cellForRowAtIndexPath:path];
cell.contentView.backgroundColor = [UIColor whiteColor];
}
try this
#property (nonatomic, assign) NSInteger selectedRow;
self.selectedRow = -1; // init with -1
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// ...
if(self.selectedRow == indexPath.row) {
cell.backgroundColor = SELECTED_COLOR;
}
else {
cell.backgroundColor = NORMAL_COLOR;
}
return cell;
}
-(void)aMethod:(UIButton*)sender {
// ...
self.selectedRow = row;
[self.myTableView reloadData];
}
I am having a radio button in table view cell. This is my radio button
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"myCell"];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"myCell"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UIButton *newRadioButton = [UIButton buttonWithType:UIButtonTypeCustom];
newRadioButton.frame = CGRectMake(30, 0, 15, 14.5);
[newRadioButton setImage:[UIImage imageNamed:#"unselect"] forState:UIControlStateNormal];
[newRadioButton setImage:[UIImage imageNamed:#"select"] forState:UIControlStateSelected];
cell.accessoryView = newRadioButton;
if ([indexPath isEqual:selectedIndex])
{
newRadioButton.selected = YES;
}
else
{
newRadioButton.selected = NO;
}
}
cell.textLabel.text = [array objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
selectedIndex = indexPath;
[table reloadData];
}
-(void)radiobtn:(id)sender
{
if([sender isSelected])
{
[sender setSelected:NO];
} else
{
[sender setSelected:YES];
}
}
Its working,But i want to select only one radio button at a time like this image (left). But for me selecting all the radio button (right).
I need to make it look like the first image.
I think you should try:
in YourViewController.h
#interface YourViewController : UITableViewController {
NSIndexPath *selectedIndex
}
in YourViewController.m
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"myCell"];
if(cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"myCell"];
UIButton *newRadioButton;
newRadioButton = [UIButton buttonWithType:UIButtonTypeCustom];
newRadioButton.frame = CGRectMake(30, 0, 15, 14.5);
[newRadioButton setImage:[UIImage imageNamed:#"unselect"] forState:UIControlStateNormal];
[newRadioButton setImage:[UIImage imageNamed:#"select"] forState:UIControlStateSelected];
cell.accessoryView = newRadioButton;
}
if ([indexPath isEqual:selectedIndex]) {
newRadioButton.seleted = YES;
} else {
newRadioButton.seleted = NO;
}
cell.textLabel.text = [array objectAtIndex:indexPath.row];
return cell;
}
and
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
selectedIndex = indexPath;
[tableView reloadData];
}
this link may help you
Unselect all the radio buttons before your if statement using something like this:
for (UITableViewCell *cell in [self visibleCells]) {
[[cell accessoryView] setSelected:NO];
}
When you are creating the button in cellForRowAtindexpth, attach an unique feature by which you can identify which button is being tapped. To do this UIButton has tag property
you need to set the tag for everybutton inside cellForRowAtindexpth, something like
// where i is just a static int variable for holding the count
myRadioButton.tag = i ;
i=i+1
now you when you tap based on tag, you can check which button is pressed and you can select that one
to know which button is pressed u can know like this
-(IBAction)buttonPressed:(id)sender{
UIButton *button = (UIButton *)sender;
NSLog(#"%d", [button tag]);
}
Try to reuse button
Solution: we can add & access controls by tag from view
if control found by tag we can use it.
if control not found we have to add it with tag.
UIButton *newRadioButton = (UIButton *)cell.accessoryView;
if (!newRadioButton || ![newRadioButton isKindOfClass:[UIButton class]]) {
UIButton *newRadioButton = [UIButton buttonWithType:UIButtonTypeCustom];
newRadioButton.frame = CGRectMake(30, 0, 15, 14.5);
[newRadioButton setImage:[UIImage imageNamed:#"unselect"] forState:UIControlStateNormal];
[newRadioButton setImage:[UIImage imageNamed:#"select"] forState:UIControlStateSelected];
cell.accessoryView = newRadioButton;
}
I want to add some buttons to custom cell, when I click the button and other buttons also change. How can I click a button without changing the other buttons?
enter code here
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = #"cell";
SViewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[SViewTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:identifier];
}
cell.cellButton.tag = indexPath.row;
[cell.cellButton addTarget:self action:#selector(clickMe:)
forControlEvents:UIControlEventTouchUpInside];
return cell;
}
- (void)clickMe:(UIButton *)sender
{
SViewTableViewCell *cell = (SViewTableViewCell *)[[sender superview]superview];
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
if (sender.tag == 0) {
if ([sender.titleLabel.text isEqualToString:openStr]) {
[sender setTitle:closeStr forState:UIControlStateNormal];
}
else{
[sender setTitle:openStr forState:UIControlStateNormal];
}
}
}
I think it's a issue about tableView cell reuse again, you should set the button title in cellForRow to keep reuse cell have a correct title, you should try code like this:
openDict is a NSMutableArray you should define, and this just a example.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = #"cell";
SViewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[SViewTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:identifier];
[cell.cellButton addTarget:self action:#selector(clickMe:)
forControlEvents:UIControlEventTouchUpInside];
}
NSString *key = [NSString stringWithFormat:#"%d-%d", indexPath.row, indexPath.section];
if (openDict[key] && [openDict[key] boolValue]) {
[sender setTitle:openStr forState:UIControlStateNormal];
}
else{
[sender setTitle:closeStr forState:UIControlStateNormal];
}
cell.cellButton.tag = indexPath.row;
return cell;
}
- (void)clickMe:(UIButton *)sender
{
SViewTableViewCell *cell = (SViewTableViewCell *)[[sender superview]superview];
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
if (sender.tag == 0) {
NSString *key = [NSString stringWithFormat:#"%d-%d", indexPath.row, indexPath.section];
if (openDict[key] && [openDict[key] boolValue]) {
openDict[key] = #(0);
[sender setTitle:closeStr forState:UIControlStateNormal];
}
else{
openDict[key] = #(1);
[sender setTitle:openStr forState:UIControlStateNormal];
}
}
}
Please set tag for each button in custom tableViewCell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = #"cell";
SViewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[SViewTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:identifier];
}
UIButton *button1 = (UIButton *)[cell viewWithTag:1];
UIButton *button2 = (UIButton *)[cell viewWithTag:2];
[button1 addTarget:self action:#selector(clickMe:)
forControlEvents:UIControlEventTouchUpInside];
[button2 addTarget:self action:#selector(clickMe:)
forControlEvents:UIControlEventTouchUpInside];
return cell;
}
To add a button to a UITableViewCell you should do it at the time you configure the cell in cellForRowAtIndexPath - this is after you get the cell to reuse. If you want to add a button to a small number of cells you should cause that cell to be reloaded using UITableView's – reloadRowsAtIndexPaths:withRowAnimation: method or reloadData to give you the opportunity to reload all cells.
Any subview you add to a cell should be added to the cell's contentView as a subview. You should remove all such views in prepareForReuse or you could find cell reuse makes extra buttons appear in cells you don't want to have buttons. Because of the need to prepare cells for reuse it is probably best to subclass UITableViewCell and to provide methods on the subclass to add buttons.
Another way is to register multiple kinds of cells for reuse with the table view, choosing what kind of cell to retrieve and use based on the data backing your table view. This is as simple as using registerClass:forCellReuseIdentifier: once for each kind of cell, then applying right reuse identifier in dequeueReusableCellWithIdentifier:forIndexPath: - you'll still be subclassing the cell objects but you configure them further in the class rather than at the time they are dequeued.
Use the code below:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = #"cell";
SViewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[SViewTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
cell.cellButton.tag = indexPath.row;
[cell.cellButton addTarget:self action:#selector(clickMe:)
forControlEvents:UIControlEventTouchUpInside];
return cell;
}
- (void)clickMe:(UIButton *)sender {
SViewTableViewCell *cell = (SViewTableViewCell *)[[[sender superview]superview] superview];
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
[self keepSelectionRemainsSameForIndexPath:indexPath andSender:sender];
}
- (void)keepSelectionRemainsSameForIndexPath:(NSIndexPath *)indexPath andSender:(UIButton *)sender {
if (sender.tag == indexPath.row) {
if ([sender.titleLabel.text isEqualToString: openStr]) {
[sender setTitle:closeStr forState:UIControlStateNormal];
} else {
[sender setTitle: openStr forState:UIControlStateNormal];
}
}
}
Am display list of names in UITableview and I'm trying to select any one of the names by using radio button. The problem I face is I can't select the specific row of the cell, either all the rows are selected or deselected.
Code for displaying radio button in cell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UIButton *report_but;
int flag;
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == Nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [user_name objectAtIndex:indexPath.row];
report_but = [UIButton buttonWithType:UIButtonTypeRoundedRect];
report_but.frame = CGRectMake(260,18,20,20);
[report_but setTitle:#"" forState:UIControlStateNormal];
if (flag == 0) {
[report_but setBackgroundImage:[UIImage imageNamed:#"radio_blank.png"] forState:UIControlStateNormal];
}
else
[report_but setBackgroundImage:[UIImage imageNamed:#"radio.png"] forState:UIControlStateNormal];
[report_but addTarget:self action:#selector(radio_button:)forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:report_but];
return cell;
}
- (void)radio_button:(id)button
{
NSLog(#"radio button clicked");
NSLog(#"button tag %#",button);
if (flag == 0) {
flag = 1;
}
else
flag = 0;
[table_list reloadData];
}
While row 1 is selected
And I can't use didselectrowatindexpath method for this single row selection, since selecting the row should not select the radio button.
Can any one help me to select the only one row of this tableviewcell.
Try this,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UIButton *report_but;
if (cell == Nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
UIButton *report_but = [UIButton buttonWithType:UIButtonTypeRoundedRect];
report_but.frame = CGRectMake(260,18,20,20);
[report_but setTitle:#"" forState:UIControlStateNormal];
report_but.tag = 8888;
[cell.contentView addSubview:report_but];
}
report_but = (UIButton *)[cell.contentView viewWithTag:8888];
if (selectedIndexPath && selectedIndexPath.row == indexPath.row) {
} else {
}
}
and in radio_button:
- (void)radio_button:(id)button
{
CGPoint pointInTable = [button convertPoint:button.bounds.origin toView:self.tableView];
selectedIndexPath = [self.tableView indexPathForRowAtPoint:pointInTable];
[table_list reloadData];
}