Assign UIButton event dynamically in UITableViewCell - iphone

I created a custom UITableViewCell subclass which contains a button named testbtn. I then tried to assign an event of that button to another class, but that isn't working for me. Please can you tell me where I am going wrong?
Here is my code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"PatientCell";
PatientCell *cell = (PatientCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
NSArray *TopLevelOb = [[NSBundle mainBundle] loadNibNamed:#"PatientCell" owner:self options:nil];
for (id currentlevelob in TopLevelOb) {
if([currentlevelob isKindOfClass:[PatientCell class]]){
cell = (PatientCell *) currentlevelob;
cell.test.text = #"some text";
cell.HR.text =#"80";
cell.ABP.text =#"120/20";
cell.SOP2.text =#"95";
cell.RR.text=#"98";
[cell.testbtn addTarget:self action:#selector(openLiveData:)
forControlEvents:UIControlEventTouchUpInside];// **this event is not fire to openLiveData method**
break;
}
}
} else {
NSArray *TopLevelOb = [[NSBundle mainBundle] loadNibNamed:#"PatientCell_iPad" owner:self options:nil];
for (id currentlevelob in TopLevelOb) {
if([currentlevelob isKindOfClass:[PatientCell class]]){
cell = (PatientCell *) currentlevelob;
cell.test.text = [nameArry objectAtIndex:indexPath.row];
cell.HR.text =#"80";
cell.ABP.text =#"120/20";
cell.SOP2.text =#"95";
cell.RR.text=#"98";
[cell.testbtn addTarget:self action:#selector(openLiveData:) forControlEvents:UIControlEventTouchUpInside];
break;
}
}
}
}
return cell;
}
-(IBAction)openLiveData:(id)sender{
NSLog(#"hello ");
}

Check to see if your button is wired up to the testBtn instance variable in your NIB.
Good Luck
T

Do this as forgot to specify Control Event:
[cell.testbtn addTarget:self action:#selector(openLiveData:) forControlEvents:UIControlEventTouchUpInside];
Hope helpful

It should be working fine in the iPhone version but should be a problem in the iPad version as you mentioned forControlEvents:UIControlStateNormal in the place of forControlEvents:UIControlEventTouchUpInside.

Related

How to Add action event work when click on customize list cell button in iphone

Create a Table View.
then create a customize table View cell and place a button on cell and Now I try to get Table View index when click on index.
But when I click on button that place on cell It's not giving me the Index of list.
My Table View class name is SubMenuViewController and cell class name is SubMenuCell
and my code of SubMenuViewController is:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ModelLocator *model = [ModelLocator getInstance];
static NSString *simpleTableIdentifier = #"SubMenuCell";
SubMenuCell *cell = (SubMenuCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"SubMenuCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
if (model.foodSubItemList) {
FoodSubItemVO* foodSubItemTemp = [model.foodSubItemList objectAtIndex:indexPath.row];
[cell.lbSubFoodItem setText: foodSubItemTemp.foodSubItemName];
[cell.lbPrice setText: foodSubItemTemp.price];
[cell setIndexPath:indexPath];
}
return cell;
}
and My SubMenuCell code here
- (IBAction)addItemIntoOrder:(id)sender {
NSLog(#"#%",indexPath);
NSLog(#"#%",indexPath.row);
}
indexPath declare in SubMenuCell.h
Your NSLog's are faulty; try this instead (mind that changed % and # order).
- (IBAction)addItemIntoOrder:(id)sender
{
NSLog(#"%#",indexPath);
NSLog(#"%#",indexPath.row);
}
Make sure that the addItemIntoOrder: action is hooked up to your delegate correctly in IB.

UITextField clears while scrolling in UITableView - custom cell

I am trying to save certain values from UITextField however, I am unable to do that because whenever I scroll there is nothing in UITextField. I am using a custom cell. I understand that custom cell has to load in a specific view and for that reason I'm using textFieldDidEndEditing to store values in an array. However, I get the following error message:
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
Help is much appreciated!! My code is below and I'm quite new in IOS Development.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0) {
if (indexPath.row == 7) {
static NSString *CellIdentifier = #"RevButtonCell";
RevButtonCell *cell = (RevButtonCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"RevButtonCell" owner:self options:nil];
cell = [topLevelObjects objectAtIndex:0];
[cell.revsaveB addTarget:self action:#selector(revaddparse:) forControlEvents:UIControlEventTouchUpInside];
cell.reveditdeleteB.hidden = YES;
cell.reveditsaveB.hidden = YES;
cell.revsaveB.hidden = NO;
cell.revdeleteB.hidden = NO;
}
return cell;
} else {
static NSString *CellIdentifier = #"TextCell";
TextCell *cell = (TextCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"TextCell" owner:self options:nil];
cell = [topLevelObjects objectAtIndex:0];
}
cell.detaillabel.text = [revdetailsA objectAtIndex:indexPath.row];
cell.detailtext.placeholder = #"Enter Text";
cell.detailtext.tag = indexPath.row;
cell.detailtext.delegate = self;
if (indexPath.row == 0) {
cell.detailtext.text = dateS;
}
if (indexPath.row == 1) {
cell.detailtext.text = clientS;
}
if (indexPath.row == 2) {
cell.detailtext.text = productS;
}
if (indexPath.row == 3) {
cell.detailtext.text = amountS;
}
if (indexPath.row == 4) {
cell.detailtext.text = qtyS;
}
if (indexPath.row == 5) {
cell.detailtext.text = taxS;
}
if (indexPath.row == 6) {
cell.detailtext.text = totalS;
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
}
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[client insertObject:clientS atIndex:0];
[product insertObject:productS atIndex:0];
[date insertObject:dateS atIndex:0];
[amount insertObject:amountS atIndex:0];
[qty insertObject:qtyS atIndex:0];
[tax insertObject:taxS atIndex:0];
[total insertObject:totalS atIndex:0];
}
Initialize the vales of to empty string as
clientS = #"";
productS = #"";
dateS = #"";
amountS = #"";
qtyS = #"";
taxS = #"";
totalS = #"";
just use every CellIdentifier are different use CellIdentifier like bellow
and also here after use this code ,not required to create array and insert object in it so dont create array just use this code..
NSString *CellIdentifier = [NSString stringWithFormat:#"Cell%d%d",indexPath.section,indexPath.row];
use this CellIndentifier insted of another like "TextCell" and "ButtonCell"
i hope this help you...
:)
It looks from your code like you are trying to implement static cells in a table view. What you might want to consider is using a storyboard and creating a table view that supports static table cells just using Interface Builder. This will allow you to eliminate the entire cellForRowAtIndexPath: method. If you can't use storyboards, then you will have to manage your cells the way you're doing it, but in my book, anytime you can eliminate code, you should.
If you're going to use a storyboard, there a couple things you should know:
Your view controller in the storyboard needs to be a table view controller
You set the table Content field to "Static Cells"
Add the textfields you want to use to each of the cells by dragging them into the cells you add
Create outlets in your view controller header and then drag connections to them in interface builder
There are some tutorials out there on using table views with static cells. They can be a little long, however, stick with them and you should be able to produce the results you're looking for.
Best regards.

UITableViewCell is hidden.

I have a UITableView in my controller. The cells for the UITableView have xib.
For some reason, when the table is loaded, the views of the cells are hidden.
I can select the cell, I see that the cell is not nil, and the views are not nil as well, still the cell is hidden.
Here is the code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
NSString *cellIdentifier = #"CategoryCell";
CategoryCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil){
NSArray* topObjects = [[NSBundle mainBundle] loadNibNamed:cellIdentifier owner:self options:nil];
for (id obj in topObjects){
if ([obj isKindOfClass:[CategoryCell class]]){
cell = (CategoryCell*)obj;
}
}
}
id object;
if (indexPath.row < items.count)
object = [items objectAtIndex:indexPath.row];
if ([object isKindOfClass:[MenuCategory class]]) {
// Configure the cell
MenuCategory *cellInfo = (MenuCategory *)object;
[cell setCategory:cellInfo];
}
else if([object isKindOfClass:[MenuSubCategory class]]){
// Configure the cell
MenuSubCategory *cellInfo = [self.items objectAtIndex:indexPath.row];
[cell setSubCategory:cellInfo];
}
return cell;
}
First:
NSArray* topObjects = [[NSBundle mainBundle] loadNibNamed:cellIdentifier owner:self options:nil];
for (id obj in topObjects){
if ([obj isKindOfClass:[CategoryCell class]]){
cell = (CategoryCell*)obj;
}
}
Replace this with
[[NSBundle mainBundle] loadNibNamed:cellIdentifier owner:self options:nil];
and declare an IBOutlet in your class that becomes the owner of the xib. This outlet can point to your cell. You then have to connect the Files Owner (has to be of the class the above code is in) within the Cell XIB to your cell. You can then reference your cell (after having declared a property for example) by
self.myCellOutlet;
and don't have to enumerate on all the objects within the xib.
Are you sure that this
[cell setSubCategory:cellInfo];
works?
If you really think your cell is just hidden, did you try to send it a
[cell setHidden:NO];
just in order to see if that's the case and if the error isn't different from the visibility state?
O so sorry wasting your time.
The solution is nothing to do with code.
The table was narrower then the cells. So the text (that was written on the left side of the cell) was outside of the table bounds. And that is why it was missing from the screen.

EXC_BAD_ACCESS When I click a button inside a custom UITableViewCell

I build a TableView with custom TableViewCell, the first custom cell have a tool bar and some Bar button Item and also a simple button for test.
The problem is : When I click in any bar button item or into simple button I have a EXC_BAD _ACCESS ?
This is my code to build the tableview cells :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"taskCell";
if(indexPath.row != 0){
TaskCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray* views = [[NSBundle mainBundle] loadNibNamed:#"TaskCell" owner:nil options:nil];
for (UIView *view in views) {
if([view isKindOfClass:[UITableViewCell class]])
{
cell = (TaskCell*)view;
}
}
}
return cell;
}
else{
if (travelInfoCell == nil) {
NSArray* views = [[NSBundle mainBundle] loadNibNamed:#"TravelInfo" owner:nil options:nil];
for (UIView *view in views) {
if([view isKindOfClass:[UITableViewCell class]])
{
travelInfoCell = (TravelInfo*)view;
}
}
}
travelInfoCell.selectionStyle = UITableViewCellSelectionStyleNone;
return travelInfoCell;
}
}
I planed to delegate the action code to the TableViewController, but for the instant I can not even hit the breakpoint in front of the button IBAction.
Code into the TravelInfo.m and is not working and throw to me EXC_BAD _ACCESS :
- (IBAction)doAccepted:(id)sender {
NSLog(#"accepted");
//[delegate travelAccepted];
}
Do have any solution ?
Here's your problem:
travelInfoCell = (TravelInfo*)view;
You are not keeping a reference to the view, so there is an implicit limited life expectancy to the view variable. You'll need to retain to keep a reference. Do this:
travelInfoCell = [(TravelInfo*)view retain];
Don't forget to release the cell and nil it out on viewDidUnload

How do you use custom UITableViewCell's effectively?

I am currently making a custom UITableView cell as shown below.
The custom UITableViewCell is in its own nib file that I am calling from another ViewController. (like so)
// RegistrationViewController.m
//Sets number of sections in the table
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
// Sets the number of rows in each section.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}
//Loads both Custom cells into each section
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//Registration Cell
static NSString *CellIdentifier = #"CustomRegCell";
static NSString *CellNib = #"LogInCustomCell";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellNib owner:self options:nil];
cell = (UITableViewCell *)[nib objectAtIndex:0];
}
//Registration Button
static NSString *CellButtonIdentifier = #"CustomSubmitCell";
static NSString *CellButtonNib = #"LogInSubmitButton";
UITableViewCell *cellButton = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellButtonIdentifier];
if (cellButton == nil) {
NSArray *nibButton = [[NSBundle mainBundle] loadNibNamed:CellButtonNib owner:self options:nil];
cellButton = (UITableViewCell *)[nibButton objectAtIndex:0];
}
if (indexPath.section == 0) {
cell.selectionStyle = UITableViewCellSelectionStyleNone; //Stops the UITableViewCell from being selectable
[self registrationControll];
//TODO: call method that controls this cell
return cell;
}
if (indexPath.section == 1) {
cellButton.selectionStyle = UITableViewCellSelectionStyleNone; //Stops the UITableViewCell from being selectable
return cellButton;
}
return nil;
}
It has four text fields that I am wanting to limit the size of the string that can be entered to five. (I'm only trying it with the first text field so far but its not even entering the textField:shouldChangeCharactersInRange:replacementString: delegate method (found this out while debugging the app) here is the code for the part I am trying to restrict the amount of characters that can be entered.
// RegistrationViewController.m
//textField:shouldChangeCharactersInRange:replacementString:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
int length = [regFieldOne.text length] ;
if (length >= MAXLENGTH && ![string isEqualToString:#""]) {
regFieldOne.text = [regFieldOne.text substringToIndex:MAXLENGTH];
return NO;
}
return YES;
}
I think I have limited my error to one of two things.
Maybe I haven't set everything up in interface builder correctly.
OR it has something to so with delegation... which I have a general understanding of and is why I think the issue might be here, but with such a complex file structure I'm not sure how or if this is right.
Any help, explanations, suggestions etc would be greatly appreciated.
At some point, you need to set the delegate for the textField
Since you put the delegate method in RegistrationViewController.m, you can set the delegate right after adding the cell in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath.
As long as you are returning a subclass of UITableViewCell from LogInCustomCell.xib, you can use something like this:
LogInCustomCell *cell = (LogInCustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellNib owner:self options:nil];
cell = (LogInCustomCell *)[nib objectAtIndex:0];
}
cell.textField1.delegate = self;
cell.textField2.delegate = self;
cell.textField3.delegate = self;
cell.textField4.delegate = self;
...
return cell;
From what I can see
You have the delegate methods in RegistrationViewController.m
But you are idnicating that CustomRegCell is the delegate, so the delegate methods should be there