Button in Cell and move View Controller - ios

I want move to View Controller after click button in cell. I try it with using click button:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSInteger rowOfCell = [indexPath row];
TwoParramCell *cell = [tableView dequeueReusableCellWithIdentifier:#"TwoParramCell" forIndexPath:indexPath];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
NSDictionary *dic = [self.arrayGoodInfo objectAtIndex:indexPath.row];
cell.delegate = self;
if (rowOfCell == 0) {
cell.btnSearch.hidden = NO;
}
cell.btnSearch.tag = indexPath.row;
[cell.btnSearch addTarget:self action:#selector(searchBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
cell.lblTitle.text = [dic objectForKey:#"stockModelName"];
cell.isMeterial = #"NO";
cell.txtContent.text = #"";
cell.txtContent.tag = indexPath.row;
if ([dic objectForKey:#"serial"]) {
cell.txtContent.text = [dic objectForKey:#"serial"];
}else{
cell.txtContent.text = #"";
}
return cell;}
Here is searchBtnClicked:
- (void)searchBtnClicked: (UIButton *) sender{
if (sender.tag == 0){
SearchSerialVC *searchVC = [[SearchSerialVC alloc] initWithNibName:#"SearchSerialVC" bundle:nil];
[self.navigationController pushViewController:searchVC animated:YES];
[VTService showToastWithText:#"Search Serial"];
}
}
Toast show but not push to Search Serial View Controller. What happen in here? Please, help me.

Step1. Comment out [VTService showToastWithText:#"Search Serial"];
Step2. Check self.navigationController
Step3. Check SearchSerialVC
self.navigationController may be is nil

Related

How to send data by clicking UITableViewCell's button to another ViewController

I want to send data using a button click which is in uitableviewcell. I got the data from web service. Each row button send different data to viewController.
Attach cellForRowAtIndex Method below:
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
static NSString *identifier = #"CategoryCell";
NSDictionary *dictMenuData = [_arrHandMadeList objectAtIndex:indexPath.row];
CategoryCell *cell = (CategoryCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
if(!cell) {
[tableView registerNib:[UINib nibWithNibName:#"CategoryCell" bundle:nil] forCellReuseIdentifier:identifier];
cell = (CategoryCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
}
cell.btnShowRatingDetails.tag = indexPath.row;
[cell.btnShowRatingDetails addTarget:self action:#selector(showRatingClicked:) forControlEvents:UIControlEventTouchUpInside];
[arrReview removeAllObjects];
//arrReview = [dictMenuData objectForKey:#"RivewData"];
[arrReview addObjectsFromArray:[dictMenuData objectForKey:#"RivewData"]];
[cell setupHandMadeCell:dictMenuData];
return cell;
}
Method for button is below:
-(void) showRatingClicked:(UIButton*)sender {
//if (sender.tag == currentIndex) {
ReviewVC *rvc = (ReviewVC*)[self.storyboard instantiateViewControllerWithIdentifier:#"ReviewVC"];
rvc.arrReviewDetails = arrReview;
rvc.strRestaurentName = [dictMenuData objectForKey:#"Restaurant_Name"];
rvc.strRestaurentId = [dictMenuData objectForKey:#"Restaurant_Id"];
rvc.strRestaurentRating = [dictMenuData objectForKey:#"Stars"];
[self.navigationController pushViewController:rvc animated:YES];
//}
}
Please help me
Thanks in advance
You need to get the index of selected cell button. You can get it from sender.tag.
-(void) showRatingClicked:(UIButton*)sender {
ReviewVC *rvc = (ReviewVC*)[self.storyboard instantiateViewControllerWithIdentifier:#"ReviewVC"];
NSDictionary *dictMenuData = [_arrHandMadeList objectAtIndex:sender.tag];
rvc.arrReviewDetails = [dictMenuData objectForKey:#"RivewData"]; //or as per your data
rvc.strRestaurentName = [dictMenuData objectForKey:#"Restaurant_Name"];
rvc.strRestaurentId = [dictMenuData objectForKey:#"Restaurant_Id"];
rvc.strRestaurentRating = [dictMenuData objectForKey:#"Stars"];
[self.navigationController pushViewController:rvc animated:YES];
}
Once cell.btnShowRatingDetails.tag = indexPath.row; button is assigned the tag you can get the data object in the action method, same way you get it using indexpath.row.
You need to use NSDictionary *dictReview = [_arrHandMadeList objectAtIndex:sender.tag]; in -(void) showRatingClicked:(UIButton*)sender to get the appropriate dictionary, then use it at the place of dictMenuData.
Remove dictMenuData altogether from your code.

Objective-C pass tableView indexPath to another method

I have this cellForRowAtIndexPath method here:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
if (cell.accessoryView == nil) {
cell.accessoryView = [[Checkbox alloc] initWithFrame:CGRectMake(0, 0, 25, 43)];
cell.accessoryView.opaque = NO;
cell.backgroundColor = [UIColor clearColor];
[(Checkbox*)cell.accessoryView addTarget:self action:#selector(checkBoxTapped:forEvent:) forControlEvents:UIControlEventValueChanged];
}
NSString *sectionTitle = [contactSectionTitles objectAtIndex:indexPath.section];
NSArray *sectionContacts = [contactDirectoryFinal objectForKey:sectionTitle];
NSString *contacts = [[sectionContacts objectAtIndex:indexPath.row] valueForKey:#"item"];
cell.textLabel.text = contacts;
[(Checkbox*)cell.accessoryView setChecked: [[[sectionContacts objectAtIndex:indexPath.row] valueForKey:#"checkedItem"] boolValue] ];
return cell;
}
inside this method, I have this line:
[(Checkbox*)cell.accessoryView addTarget:self action:#selector(checkBoxTapped:forEvent:) forControlEvents:UIControlEventValueChanged];
which calls this method
- (IBAction)checkBoxTapped:(id)sender forEvent:(UIEvent*)event
{
}
What I am trying to do is adjust the way this method is called so I am also passing int the indexPath.row number
I got started with this:
[(Checkbox*)cell.accessoryView addTarget:self action:#selector(checkBoxTapped:forEvent:rowNumber:) forControlEvents:UIControlEventValueChanged];
and this
- (IBAction)checkBoxTapped:(id)sender forEvent:(UIEvent*)event rowNumber:(int)row
{
}
but I am new to selectors, my question is where do I pass in the rowNumber?
As you've found out you can't pass just any values with your selector, only the sender and the event. This is why you need another way pass the row number.
One method is to use the tag property on your control and retrieve it with [sender tag], however this approach will fail if the table indexes are changed without cellForRowAtIndexPath being called. It also won't work for sectioned table views.
A more robust approach is to use the location of your view to calculate the correct row. UITableView has a convenient method to do this:
- (IBAction)checkBoxTapped:(id)sender forEvent:(UIEvent*)event {
CGPoint checkBoxPosition = [sender convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:checkBoxPosition];
}
As an alternative to passing it in ....
func checkBoxTapped(sender: CheckBox, withEvent event: UIEvent) {
if let tap = event.allTouches()?.first {
let location = tap.locationInView(tableView)
let indexPath = tableView.indexPathForRowAtPoint(location)
..... do something
}
}
*Update
As #Maddy 's comment
Do not use tag to represent the indexPath. It fails if the table view allows any rows to be moved, inserted, or deleted
this is not a good answer.*
As CheckBox is a UIView you can set its tag in cellForRowAtIndexPath: and then check for it in checkBoxTapped:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
if (cell.accessoryView == nil) {
cell.accessoryView = [[Checkbox alloc] initWithFrame:CGRectMake(0, 0, 25, 43)];
cell.accessoryView.opaque = NO;
cell.backgroundColor = [UIColor clearColor];
[(Checkbox*)cell.accessoryView addTarget:self action:#selector(checkBoxTapped:forEvent:) forControlEvents:UIControlEventValueChanged];
}
NSString *sectionTitle = [contactSectionTitles objectAtIndex:indexPath.section];
NSArray *sectionContacts = [contactDirectoryFinal objectForKey:sectionTitle];
NSString *contacts = [[sectionContacts objectAtIndex:indexPath.row] valueForKey:#"item"];
cell.textLabel.text = contacts;
[(Checkbox*)cell.accessoryView setChecked: [[[sectionContacts objectAtIndex:indexPath.row] valueForKey:#"checkedItem"] boolValue] ];
// set the index to the tag.
cell.accessoryView.tag = indexPath.row
return cell;
}
- (IBAction)checkBoxTapped:(id)sender forEvent:(UIEvent*)event
{
// get the index from the tag value
NSInteger row = ((UIView *)sender).tag
}
You could use:
- (NSIndexPath *)indexPathForCell:(UITableViewCell *)cell
once you have the UITableViewCell object which can be accessed as sender.superview

checkBox action event in uitableviewcell

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];
}

How to identify the row index and section number of a clicked button in a custom cell inside tableview

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *simpleTableIdentifier = #"CustomCell";
cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner: self options: nil ];
cell = [nib objectAtIndex:0];
}
//cell.lblName.text = [items objectAtIndex:indexPath.row];
NSMutableDictionary *dic = [items objectAtIndex:indexPath.row];
cell.imgface.image = [dic objectForKey:#"Image"];
cell.imgface.layer.cornerRadius = cell.imgface.frame.size.width / 2;
cell.imgface.layer.borderWidth = 3.0f;
cell.imgface.layer.borderColor = [UIColor whiteColor].CGColor;
cell.imgface.clipsToBounds = YES;
cell.lblName.text = [dic objectForKey:#"Name"];
[cell.btnPhone setTitle:[dic objectForKey:#"Phone"] forState:(UIControlStateNormal)];
cell.btnPhone.tag = indexPath.row;
cell.btnstar.tag = indexPath.row;
[cell.btnPhone addTarget:self action:#selector(dial:) forControlEvents:(UIControlEventTouchUpInside)];
[cell.btnstar addTarget:self action:#selector(toggleimage:) forControlEvents:UIControlEventTouchUpInside];
if (indexPath.section == 0)
{
star = [UIImage imageNamed:#"Star-Favorites.png"];
[cell.btnstar setBackgroundImage:star forState:UIControlStateNormal];
}
else
{
star = [UIImage imageNamed:#"keditbookmarks.png"];
[cell.btnstar setBackgroundImage:star forState:UIControlStateNormal];
}
return cell;
}
write below code in button IBAction and you will get indexpath.row and indexpath.section
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
NSLog(#"selected tableview row is %d",indexPath.row);
You have some different ways to do that:
• When you configure the cell set the tag of the cell or the button as the row and retrieve that on the IBAction (I hate tags though)
• Define a delegate for the table cell and set it when you configure it. Then call the delegate method passing the cell and find the relative index (-indexPathForCell:)
• The same as before but pass the cell through a notification instead of delegates
first of all on, button IBAction call this method
- (IBAction)btnClick:(UIButton)sender
{
UITableViewCell *cellOfcontrol=[self cellWithSubview:sender];
NSIndexPath *indexPath = [self.tableview indexPathForCell:cellOfcontrol];
}
cellWithSubview function
- (UITableViewCell *)cellWithSubview:(UIView *)subview
{
while (subview && ![subview isKindOfClass:[UITableViewCell self]])
subview = subview.superview;
return (UITableViewCell *)subview;
}

Custom cells reload issue while scrolling UITableView

I have a UITableView where in i am using a custom UITableViewCell. All is well until the UITableView is scrolled. As soon as i scroll the UITableView the cells in Second section starts showing the content of cell in the first section.
I am using Storyboard for my project.
Here is the code in cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
LS_Custom_Cell *customCell = (LS_Custom_Cell *)[tableView dequeueReusableCellWithIdentifier:#"LS_Custom_Cell" forIndexPath:indexPath];
if (indexPath.section == 0 && indexPath.row == 0) {
customCell.selectionStyle = UITableViewCellSelectionStyleNone;
[customCell.btnAddContact addTarget:self
action:#selector(btnAddContactAction:)
forControlEvents:UIControlEventTouchDown];
customCell.btnSelectContact.hidden = YES;
customCell.btnAddContact.hidden = NO;
customCell.lblAddContactText.hidden = NO;
customCell.lblContactName.hidden = YES;
customCell.lblContactEmailId.hidden = YES;
}else if (indexPath.section == 1){
customCell.lblContactName.text = [NSString stringWithFormat:#"%#", [[contactsArray objectAtIndex:indexPath.row] valueForKey:#"name"]];
customCell.lblContactEmailId.text = [NSString stringWithFormat:#"%#", [[contactsArray objectAtIndex:indexPath.row] valueForKey:#"email"]];
}
return customCell;
}
I am also attaching images in order to clarify the issue further.
This screenshot is the first time when table loads records seems perfect
http://postimg.org/image/y2114vjdl/
As soon as i start scrolling cell from the first section appears in second section
http://postimg.org/image/c5ei4i66x/
This is first time i am posting a question here so please forgive me if there are any mistakes. Any help in this regard would be very much appreciated. Thanks in advance.
Try this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
LS_Custom_Cell *customCell = (LS_Custom_Cell *)[tableView dequeueReusableCellWithIdentifier:#"LS_Custom_Cell" forIndexPath:indexPath];
if (indexPath.section == 0 && indexPath.row == 0)
{
customCell.selectionStyle = UITableViewCellSelectionStyleNone;
[customCell.btnAddContact addTarget:self
action:#selector(btnAddContactAction:)
forControlEvents:UIControlEventTouchDown];
customCell.btnSelectContact.hidden = YES;
customCell.btnAddContact.hidden = NO;
customCell.lblAddContactText.hidden = NO;
customCell.lblContactName.hidden = YES;
customCell.lblContactEmailId.hidden = YES;
}
else if (indexPath.section == 1)
{
customCell.btnSelectContact.hidden = NO;
customCell.btnAddContact.hidden = YES;
customCell.lblAddContactText.hidden = YES;
customCell.lblContactName.hidden = NO;
customCell.lblContactEmailId.hidden = NO;
customCell.lblContactName.text = [NSString stringWithFormat:#"%#", [[contactsArray objectAtIndex:indexPath.row] valueForKey:#"name"]];
customCell.lblContactEmailId.text = [NSString stringWithFormat:#"%#", [[contactsArray objectAtIndex:indexPath.row] valueForKey:#"email"]];
}
return customCell;
}

Resources