在DetailViewController中点击按钮时,在单个UITableViewCell内部更改图像

时间:2016-11-21 19:22:21

标签: ios objective-c uitableview

在我的详细视图控制器(DetailViewController.m)中,会触发更改UITableViewCell类型图像的通知。我有两种类型的UITableViewCells。也就是说,当tapButton或declineButton被点击时,我只想让图像在1个特定单元格中更改(具体地说,是在打开细节视图的抽头单元格中的图像 - 而不是在该类型的所有单元格中)。我该怎么做呢?到目前为止,这是我的代码:

DetailViewController.m

  - (IBAction)acceptButton:(id)sender {

        if (!checked1) {

            [self.acceptCheck setImage:[UIImage imageNamed:@"checkedgreen.png"] forState:UIControlStateNormal];

            checked1 = YES;

             [[NSNotificationCenter defaultCenter] postNotificationName:@"ImageChangeNotification" object:nil];

            AcceptedViewController *detailViewController = [[AcceptedViewController alloc]
                                                               initWithNibName:@"AcceptedViewController" bundle:nil];


            [self presentViewController:detailViewController animated:YES completion:nil];

        }

        else if (checked1) {

            [self.acceptCheck setImage:[UIImage imageNamed:@"uncheckedgray.png"] forState:UIControlStateNormal];
            checked1 = NO;

        }

    }

- (IBAction)declineButton:(id)sender {

    if (!checked2) {

        [self.declineCheck setImage:[UIImage imageNamed:@"declinered.png"] forState:UIControlStateNormal];
        checked2 = YES;

        [[NSNotificationCenter defaultCenter] postNotificationName:@"Declined" object:nil];

        DeniedViewController *detailViewController = [[DeniedViewController alloc]
                                                            initWithNibName:@"DeniedViewController" bundle:nil];


        [self presentViewController:detailViewController animated:YES completion:nil];

    }

    else if (checked2) {

        [self.declineCheck setImage:[UIImage imageNamed:@"declinegray.png"] forState:UIControlStateNormal];
        checked2 = NO;

    }
}

TableViewCellB.m

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(changeImage)
                                                 name:@"ImageChangeNotification"
                                               object:nil];


    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(declineImage)
                                                 name:@"Declined"
                                               object:nil];   
}

-(void)changeImage
{
    [self.displayedBar setImage:[UIImage imageNamed:@"greenbar.png"]];
    self.Title.text = @"Accepted!";

}


-(void)declineImage
{
    [self.displayedBar setImage:[UIImage imageNamed:@"redbar.png"]];
    self.Title.text = @"Declined!";

}

TableViewController.m (如果需要)

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {


        NSDictionary *data = self.messages[indexPath.row];

        id swaptime = data[@"swaptime"];
        if ([swaptime isKindOfClass:[NSString class]]) {

            static NSString *ChatTableIdentifier2 = @"SwapDetailTableViewCell";

            SwapDetailTableViewCell *cell = (SwapDetailTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ChatTableIdentifier2 forIndexPath:indexPath];

            NSString *time = data[@"swaptime"];
            cell.startTime.text = time;

            NSString *timeEnd = data[@"endswaptime"];
            cell.endTime.text = timeEnd;

            NSString *costofSwap = data[@"swapvalue"];
            cell.swapValue.text = costofSwap;
      return cell;

        } else {
            static NSString *ChatTableIdentifier = @"ChatTableViewCell";

            ChatTableViewCell *cell = (ChatTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ChatTableIdentifier forIndexPath:indexPath];

            NSString *userName = data[@"name"];
            cell.sendingUser.text = userName;

            NSString *messageBody = data[@"body"];
            cell.messageDisplayed.text = messageBody;

            NSString *timeReceived = data[@"published at"];
            cell.timeStamp.text = timeReceived;


            NSString *userInfo = [self.userid objectForKey:@"name"];


            if ([cell.sendingUser.text isEqual: userInfo]) {

                cell.messageDisplayed.textAlignment = NSTextAlignmentLeft;
                cell.sendingUser.textAlignment = NSTextAlignmentLeft;


                [cell.chatBubble setImage:[UIImage imageNamed:@"bubblegrey2.png"]];


            } else {

             cell.messageDisplayed.textAlignment = NSTextAlignmentRight;
             cell.sendingUser.textAlignment = NSTextAlignmentRight;


                [cell.chatBubble setImage:[UIImage imageNamed:@"bubbleorange2.png"]];

            }


            return cell;
        }
    }

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {


    NSDictionary *data = self.messages[indexPath.row];

    id swaptime = data[@"swaptime"];

    if ([swaptime isKindOfClass:[NSString class]]) {

    SwapDetailsViewController *detailViewController = [[SwapDetailsViewController alloc]
                                                        initWithNibName:@"SwapDetailsViewController" bundle:nil];

        detailViewController.indexPath = [self.tableView indexPathForSelectedRow];
        detailViewController.delegate = self;


    detailViewController.swapDetails = [self.messages objectAtIndex:indexPath.row];

  [self presentViewController:detailViewController animated:YES completion:nil];

    } else {


    }

}

2 个答案:

答案 0 :(得分:0)

@interface DetailViewController ()
{
    UIImage *_yourImage;
}

然后在declineButtonacceptButton中更改您需要的内容并执行[tableView reloadData]

答案 1 :(得分:0)

在DetailViewController.h中创建协议并创建它的属性,如下所示。

@protocol DetailViewControllerDelegate <NSObject>

- (void)acceptButton:(BOOL)accepted withIndexPath:(NSIndexPath*)indexPath;

@end

@interface DetailViewController : UIViewController

@property (strong, nonatomic) NSIndexPath *indexPath;
@property (weak, nonatomic) id<DetailViewControllerDelegate> delegate;
// other properites and methods ....

@end

在MasterViewController中

推送detailViewController时,传递indexpath并将delegete设置为self并确认,实现委托方法,如下所示

@interface MasterViewController () <DetailViewControllerDelegate>
// other properties....
@end

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([[segue identifier] isEqualToString:@"showDetail"]) {

        DetailViewController *controller = (DetailViewController *)[[segue destinationViewController] topViewController];
        controller.indexPath = [self.tableView indexPathForSelectedRow];
        controller.delegate = self;
        // Other code...
    }
}

- (void)acceptButton:(BOOL)accepted withIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    if (accepted) {
        // do accepted stuff
    } else {
        // do decline stuff
    }
}

IN DetailViewController.m

实施接受和拒绝按钮操作

 - (IBAction)acceptButton:(id)sender {

    if (!checked1) {
        [self.acceptCheck setImage:[UIImage imageNamed:@"checkedgreen.png"] forState:UIControlStateNormal];
        checked1 = YES;
        /// calling delegate method
        [self.delegate acceptButton:YES withIndexPath:self.indexPath];
        AcceptedViewController *detailViewController = [[AcceptedViewController alloc]
                                                        initWithNibName:@"AcceptedViewController" bundle:nil];
        [self presentViewController:detailViewController animated:YES completion:nil];

    } else if (checked1) {
        [self.acceptCheck setImage:[UIImage imageNamed:@"uncheckedgray.png"] forState:UIControlStateNormal];
        checked1 = NO;
    }
}

- (IBAction)declineButton:(id)sender {

    if (!checked2) {
        [self.declineCheck setImage:[UIImage imageNamed:@"declinered.png"] forState:UIControlStateNormal];
        checked2 = YES;
        /// calling delegate method
        [self.delegate acceptButton:NO withIndexPath:self.indexPath];
        DeniedViewController *detailViewController = [[DeniedViewController alloc]
                                                      initWithNibName:@"DeniedViewController" bundle:nil];
        [self presentViewController:detailViewController animated:YES completion:nil];
    } else if (checked2) {
        [self.declineCheck setImage:[UIImage imageNamed:@"declinegray.png"] forState:UIControlStateNormal];
        checked2 = NO;
    }
}