Reputation: 101
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
Upvotes: 2
Views: 1117
Reputation: 5247
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;
}
Upvotes: 2
Reputation: 613
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.
Upvotes: 0