简体   繁体   中英

How detect indexPath on click button at row?

I have this UItableView :

在此处输入图片说明

I would like to increment/decrement the concrete UIProgressView at row, when I clicked on minus/plus and than also reload, whole UITableView , because all rows are interdependent. I don't how detect on which row was pressed the minus/plus UIButton . I tried to use rowDidSelect, but it didn't work. Could you please advise me please?

EDITED

I have a class for UITableCell (ActivityCell - class) and class for UITableViewController (lessonActivityTVC - class). At class of Cell I have IBOutlets .

ActivityCell.h
@interface ActivityCell : UITableViewCell
@property (strong, nonatomic) IBOutlet UILabel *studentNameLabel;
@property (strong, nonatomic) IBOutlet UIProgressView *progressView;

In ActivityCell.m I have nothing new. I had here a IBActions for buttons, but I moved the actions into lessonActivity.m because, when I press any button I need reload the UITableView and I didnt know how do it in ActivityCell.m . My code of lessonActivityTVC.m is here:

@implementation tchLessonActivityTVC
#define debug 1

- (IBAction)incrementActivity:(id)sender {

    NSLog(@"increment");
}
- (IBAction)decrementActivity:(id)sender {
    NSLog(@"decrement");
}

-(void)fetchData{
    CoreDataHelper *cdh = [(AppDelegate*)[[UIApplication sharedApplication]delegate]cdh];
    SchoolClass *schoolClass = (SchoolClass*)[cdh.context existingObjectWithID:[IDsManager getClassID] error:nil];
    Lesson *lesson = (Lesson*)[cdh.context existingObjectWithID:[IDsManager getLessonID] error:nil];

    if (lesson.activities.count == 0) {
        for (Student *student in schoolClass.students) {
            Activity *activity = [NSEntityDescription insertNewObjectForEntityForName:@"Activity" inManagedObjectContext:cdh.context];
            activity.student = student;
            activity.lesson = lesson;
            [lesson addActivitiesObject:activity];
            [student addActivitiesObject:activity];
        }
    }
    self.activities = [NSArray arrayWithArray:[lesson.activities allObjects]];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.tableView.delegate = self;
    [self fetchData];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.activities.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(debug == 1){
        NSLog(@"Running %@ '%@'",self.class, NSStringFromSelector(_cmd));
    }

    static NSString *cellIndetifier = @"Activity Cell";
    ActivityCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndetifier
                                                         forIndexPath:indexPath];

    Activity *activity = [self.activities objectAtIndex:indexPath.row];

    cell.studentNameLabel.text = activity.student.name;
    CGAffineTransform transform = CGAffineTransformMakeScale(1.0f, 5.0f);
    cell.progressView.transform = transform;

    cell.progressView.progress = 0.32f;        
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"selected index is %d",indexPath.row);
}
@end

UITableView has a delegate method didSelectRowAtIndexPath: and you get the indexPath.row from there. Don't forget to set your class as a UITableViewDelegate by adding <UITableViewDelegate> on the end of @interface line and setting

tableView.delegate = self;

in code, or do it in Interface Builder. If you are on UITableViewController , you get this for free.

Edit:

Here is the delegate pattern example:

tableviewcell h

@protocol TableViewCellDelegate <NSObject>

- (void)requestToReloadTableViewFromCell:(UITableViewCell *)cell;

@end

@interface TableViewCell : UITableViewCell

@property (weak, nonatomic) IBOutlet UILabel *dateLabel;
@property (weak, nonatomic) id <TableViewCellDelegate> cellDelegate;

@end

tableviewcell m

- (IBAction)buttonAction:(UIButton *)sender
{
    [self.cellDelegate requestToReloadTableViewFromCell:self];
}

tableviewcontroller m

@interface TableViewController () <TableViewCellDelegate>

@end

@implementation TableViewController

- (void)requestToReloadTableViewFromCell:(UITableViewCell *)cell
{
    NSLog(@"%d", [[self.tableView indexPathForCell:cell] row]);
    [self.tableView reloadData];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

    // Configure the cell...
    cell.cellDelegate = self;
    cell.dateLabel.text = [[NSDate date] description];

    return cell;
}

you can use the tag with these buttons programatically in cellForRowAtIndexPath method. Tag should be different for each button (you can use some formula, for example tag will be equal to row no). in this way when button will be pressed you will check tag first and can know which button is pressed at which row.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM