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.