Hello. The situation is this: I have a table (Table View) and a single cell (Cell). The number of rows is determined automatically by the most typical method:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [data count]; // data - это объект-массив } 

In each row is a button (Button). I need to implement a method that, when I pressed the buttons in all the rows, launched something, for example, I would display a certain inscription. I believe that you need to work with indexPath.row, but I do not know how.

Tell me, please, how to do it.

  • Is it interesting to use UIButton or is it enough to tample around the cell? - Vitaly
  • Vitaly, namely UIButton - Bandicoot

2 answers 2

There is no cell class code in your question. And information about UITableView and its delegates is not enough. It is also unknown what kind of data you store. In this case, suppose you have the following data class interface:

 #import <Foundation/Foundation.h> @interface MyData : NSObject @property (strong, nonatomic) NSString* text; -(instancetype)initWithText:(NSString*)text; @end 

With implementation:

 #import "MyData.h" @implementation MyData -(instancetype)initWithText:(NSString*)text { self = [super init]; if (self) { self.text = text; } return self; } @end 

Your UIViewController should have the following property:

 @property (strong, nonatomic) NSArray<MyData*>* dataArray; 

You, for example, initialize it in viewDidLoad :

 self.dataArray = @[[[MyData alloc] initWithText:@"First text"], [[MyData alloc] initWithText:@"Second text"], [[MyData alloc] initWithText:@"Third text"]]; 

And then immediately update the UITableView (I hope that you have a tableView link to your table):

 [self.tableView reloadData]; 

The required minimum of the UITableViewDelegate and UITableViewDataSource methods is as follows:

 - (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath { MyTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell" forIndexPath:indexPath]; [cell setupWith:[self.dataArray objectAtIndex:indexPath.row] controller:self]; return cell; } - (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.dataArray.count; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 100.0; } 

Table Cell Interface Code:

 #import <UIKit/UIKit.h> #import "MyData.h" #import "ViewController.h" @interface MyTableViewCell : UITableViewCell //Ваша кнопка @property (strong, nonatomic) UIButton* btn; -(void)setupWith:(MyData*)data controller:(ViewController*)controller; @end 

Implementation:

 #import "MyTableViewCell.h" @interface MyTableViewCell () //Ваши абстрактные данные @property (strong, nonatomic) MyData* data; //Ссылка на контроллер, где размещена таблица (скорее всего Вам это пригодится) @property (weak, nonatomic) ViewController* controller; @end @implementation MyTableViewCell -(instancetype)initWithCoder:(NSCoder *)aDecoder { self = [super initWithCoder:aDecoder]; if (self) { [self createButton]; } return self; } -(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { [self createButton]; } return self; } -(void)createButton { self.btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 200, 50)]; self.btn.backgroundColor = UIColor.greenColor; [self.btn addTarget:self action:@selector(onBtnPressed:) forControlEvents:UIControlEventTouchUpInside]; [self.contentView addSubview:self.btn]; } -(void)onBtnPressed:(UIButton*)sender { //Здесь Вы можете вывести на экране неободимый текст или произвести иные действия UIAlertController* alert = [[UIAlertController alloc] init]; alert.message = self.data.text; [alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { }]]; [self.controller presentViewController:alert animated:YES completion:^{ }]; } -(void)setupWith:(MyData*)data controller:(ViewController*)controller { self.data = data; self.controller = controller; [self.btn setTitle:self.data.text forState:UIControlStateNormal]; } @end 

As a result, you do not need to get the sequence number of the cell. Store the necessary data in the cell itself, and then your code will work and will be clear to the rest.

  • Something I do not really understand all this. How does the computer understand what button of the cell row we pressed (the cell is one)? And yet, I need to bring a certain label, subject to pressing the buttons of all rows. If I write such code, it works when I press any one button, which is logical: - Bandicoot
  • @Bandicoot Are you going to add more code to the question? - Roman Podymov pm
  • The code I mentioned in the comment above: - (void) comleted {if (checkReq == YES) {_checkComplete.text = @ "CHECKLIST COMPLETED"; _checkComplete.textColor = [UIColor greenColor]; } else if (checkReq! = YES) {_checkComplete.text = @ ""; }} checkReq - BOOL variable - Bandicoot
  • It doesn't work, my reputation is too low. Can I still contact you? - Bandicoot
  • @Bandicoot What is the error? You are chat.stackexchange.com site. visited? - Roman Podymov

To implement such things it is not necessary to get the row number of the cell. It is much easier to do this with the help of tags that you need to override in the cellForRowAtIndexpath method

In my case, the code looks like this.

  - (UITableViewCell *)tableView:(UITableView *)tableView {cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *const CellId = @"Cell"; CheckLabelCell *cell = [tableView dequeueReusableCellWithIdentifier:CellId]; cell.checkButton.tag = indexPath.row; } 

And then write the code, starting from the button tag, for example

 -(void)On:(int)sender { NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; UIImage *Normal = [UIImage imageNamed:@"main.png"]; if (sender == 7) { [self.checkButton setBackgroundImage:checkNormal forState:UIControlStateNormal]; [defaults setBool:NO forKey:@"bool0"]; Req0 = NO; self.Label.textColor = [UIColor whiteColor]; }