objective-c - 如何检测行上单击按钮的indexPath?

标签 objective-c uibutton uitableview

我有这个UItableView:

enter image description here

当我单击减号/加号并且重新加载整个UITableView时,我想增加/减少行中的具体UIProgressView,因为所有行都是相互依赖的。我不知道如何检测在哪一行按下了减号/加号UIButton。我尝试使用 rowDidSelect,但没有成功。您能给我建议吗?

已编辑

我有一个 UITableCell 类(ActivityCell - 类)和 UITableViewController 类(lessonActivityTVC - 类)。在 Cell 类中,我有 IBOutlets

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

在 ActivityCell.m 中我没有什么新内容。我这里有一个用于按钮的 IBActions,但我将操作移至 lessonActivity.m 中,因为当我按下任何按钮时,我需要重新加载 UITableView 并且我不知道该怎么做在ActivityCell.m中。我的 LessonActivityTVC.m 代码在这里:

@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有一个委托(delegate)方法 didSelectRowAtIndexPath:然后你就可以从那里得到indexPath.row。不要忘记将您的类(class)设置为 UITableViewDelegate通过添加<UITableViewDelegate>@interface 的末尾线路和设置

tableView.delegate = self;

在代码中,或者在 Interface Builder 中执行。如果您使用 UITableViewController ,您可以免费获得这个。

编辑:

这是委托(delegate)模式示例:

表格 View 单元格

@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;
}

关于objective-c - 如何检测行上单击按钮的indexPath?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23040466/

相关文章:

ios - 通过两个一对一关系查找核心数据实体

iphone - 对象dealloc仅在iOS 4.3中崩溃

ios - 如何从 UITableViewCell (iOS5) 中删除披露 V 形?

objective-c - PrepareForSegue 与 UIImagePickerController

iphone - 图标 iPhone 应用程序

ios - xcode - 发送到实例的无法识别的选择器

ios - 如何阻止禁用的 UIbutton 执行点击操作

swift - 点击时如何停止动画

ios - 快速切换大小写

ios - 为什么 awakeFromNib 从 TableView 中的 Cell 调用两次?