Reputation:
i want to start a timer label in a custom cell, when the button in the cell was pressed. How can i display that label in the selected cell? I used tag to connect the label to my table view. Thanks
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier;
CellIdentifier = @"Cell01";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Button
Button = (UIButton *)[cell viewWithTag:104];
[Button addTarget:self action:@selector(ButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
theLabel = (UILabel *)[cell viewWithTag:105];
return cell; }
And here is the button action
-(void)ButtonClicked:(id)sender {
UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview];
NSIndexPath *clickedButtonPath = [self.TableView indexPathForCell:clickedCell];
NSInteger ClickedRow = clickedButtonPath.row;
// In the clickedButtonPath, this allow us to find out the row that was selected by the user.
NSLog(@"%@",clickedCell);
switch(ClickedRow)
{
case 1:
{
// first row
}
break;
case 2:
{
// second row
}
break;
}}
Upvotes: 1
Views: 442
Reputation: 5047
The first mistake is you are not created a custom cell, the cell you creating does´t have view 104 and 105. More in code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// this 1.-
NSString *CellIdentifier;
CellIdentifier = @"Cell01";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
// Here you create a new UITableViewCell.
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// this 2.
// Button
Button = (UIButton *)[cell viewWithTag:104];
// This is nil. You can add
NSLog(@"See this button %@",[Button description]);
// And see nil in the console.
[Button addTarget:self action:@selector(ButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
// The same thing this label
theLabel = (UILabel *)[cell viewWithTag:105];
return cell; }
If you are using Storyboards(or interface builder), in the cell in stroryboard be sure the reuseIdentifier is Cell01, and change al code between: this 1.- and this 2.- by:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell01" forIndexPath:indexPath];
The last step will be: 1.- In Storyboard or in code quit the default select behavior, in code you must add this just above the cell created.
cell.selectionStyle = UITableViewCellSelectionStyleNone;
2.- Add didSelect and undo:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
UILabel *theLabel = (UILabel *)[selectedCell viewWithTag:105];
theLabel.text = @"New Selected text";
}
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
UILabel *theLabel = (UILabel *)[selectedCell viewWithTag:105];
theLabel.text = @"Non Selected text";
}
Upvotes: 1