2

i am a newbie to iphone development.

I am working on a project where i am having a UItableview , i placed a button in each row . Now my UItableView have a cell.textlabel, detailLabel, and a button placed programatically.

In cell.textlabel i have some values from json(each label have different values) and just at the right corner of each row , i placed a button.

Now i want the cell.textlable.text value of that row only whose button is pressed.

I am getting the same value for each button. So , my question is , how to get labeltext value of the specific row whose button is pressed ?

mH16
  • 2,356
  • 4
  • 25
  • 35

4 Answers4

2

attach a tag with your button during its creation e.g. button1.tag = 9999; in your button click action grab the button as sender

-(void)buttonaction:(UIButton*)sender
{
UITableViewCell * selectedCell = nil;
for(UITableViewCell *cell in [tableView visibleCells])
{
  if([cell viewWithTag:9999] == sender)
  {
     selectedCell = cell;
     break;
  }
}
//do whatever you like with selected cell now.
}

I hope it helps!!!

Hermann Klecker
  • 14,039
  • 5
  • 48
  • 71
Saurabh Passolia
  • 8,099
  • 1
  • 26
  • 41
  • what i need is: how a button on a specific row on click, returns the value of that row's cell.textlabel value.i.e when i click the button at row no. 3, it should return textlabel value of that row only. – mH16 Sep 19 '11 at 04:15
2

Solved like this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:  (NSIndexPath *)indexPath {



static NSString *CellIdentifier = @"Cell";

cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];

    UIButton *tableCellButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect]retain];
    tableCellButton.frame = CGRectMake(200, 1, 80, 20);
    [tableCellButton setTitle:@"showing" forState:UIControlStateNormal];
    [tableCellButton addTarget:self action:@selector(shortcutBottonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [cell addSubview:tableCellButton];



}   NSDictionary *listing = [ self.temp objectAtIndex:indexPath.row];
cell.textLabel.text = [listing  objectForKey:@"0"];




return cell;

}

-(IBAction) shortcutBottonClicked:(id)sender{

  //this code solved the problem
 indexPath = [self.tableView indexPathForCell:(UITableViewCell*)[sender superview]];

mlsid2 = [self.tableView cellForRowAtIndexPath:indexPath].textLabel.text;
}
mH16
  • 2,356
  • 4
  • 25
  • 35
  • [sender superview] does not really work when the button has been added to the cell via IB (in a xib). The method will then not return an UITableViewCell, but an UITableViewCellContentView (at least on iOS 6.0) – Katlu Oct 17 '12 at 14:28
1

I think you mean that each row in the table has a button, and any number of them can be pressed. After the user is done pressing buttons for rows, you need to get which rows of data have been pressed.

If I read you correclty then unless your table is static, it will work something like the following:

You need to have a datastructure like the one I'm assuming you have for your UITableview datasource. For example, an NSArray full of NSNumber objects, one corresponding to each row of data. When the button in a row is pressed, the corresponding NSNumber is set 1. When you need it, just iterate through the NSArray of NSNumbers to know the corresponding rows in your table's datasource that had it's button pressed.

One thing to be aware of is that UItableview cells that you see in the UI are normally reused as the user scrolls - at least this is the case if you have a variable amount of data and rows for your table (as opposed to static), so you can't rely on the UI elements, like the button, for remembering the state

kris
  • 2,516
  • 25
  • 29
1

in first view .h file

@

class viewController;
#import <UIKit/UIKit.h>


@interface firstViewController : UITableViewController<UITableViewDelegate, UITableViewDataSource> {

    NSString *_cellTitle;


}

@end

first view .m file in didSelectRowAtIndex method add this code like

viewController *detailViewController = [[viewController alloc]     initWithNibName:@"viewController" bundle:nil];       

NSInteger row = [indexPath row];
_rowTitle = [_detailList objectAtIndex:row];

detailViewController._barTitle = _rowTitle ;

or you can do this in buttonPressed method

then in the second view .h section -

@interface viewController : UIViewController {
   NSString *_barTitle;
}

@property (nonatomic, retain) NSString *_barTitle;

@end

synthesize this property in implementation section in .m and under viewDidLoad method

self.navigationItem.title = _barTitle;

don't forget to import second view .h file

hope this helps

Hiren
  • 12,720
  • 7
  • 52
  • 72
XTremeHell
  • 11
  • 1