从Custom Cell调用ActivityViewController

时间:2015-01-09 22:12:17

标签: ios uitableview uiactivityviewcontroller

我的TableView应用程序有一个自定义单元格。 TableViewController被称为“BlogView”。我的自定义单元格上有几个按钮,一个是共享按钮。我想在按下其中一个按钮时显示UIActivityViewController。

在我的自定义单元格的标题中,我有一个属性:

@property (nonatomic, retain) BlogView *myViewController;

在自定义单元格中,我有layoutSubview:

self.commentButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.commentButton addTarget:self action:@selector(didTapCommentButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.commentButton setTitle:@"Share" forState:UIControlStateNormal];
    [self.commentButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.commentButton setTitleColor:[UIColor greenColor] forState:UIControlStateHighlighted];

对于选择器didTapCommentButtonAction我有:

- (void)didTapCommentButtonAction:(id)sender
{
    NSLog(@"CommentButtonTAPPED");
    Mail *mail = [[Mail alloc]init];
    NSString *html = self.prayerObject[@"Request"];
    NSString *thetitle = [self.prayerObject[@"Title"] stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
    NSString *thedate = self.prayerObject[@"dateMade"];
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"MMM_dd_yyyy"];
    [dateFormat setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
    NSDate *theNewDate1 = [dateFormat dateFromString:thedate];
    NSString *theNewDate = [dateFormat stringFromDate:theNewDate1];

    mail.thehtml = html;
    self.nameofhtmlfile = [[[[@"http://www.iprayed4u.net/app/" stringByAppendingString:thetitle] stringByAppendingString:@"_"] stringByAppendingString:theNewDate] stringByAppendingString:@".html"];
    //  Reminder *thereminder = [[Reminder alloc] init];
    //thereminder.thehtml = html;
    //thereminder.thetitle = thetitle;
    //thereminder.thedate = thedate;

    UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:@[self] applicationActivities:@[mail]];


    if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) {
        activityVC.excludedActivityTypes = @[ UIActivityTypePostToWeibo,

                                              UIActivityTypeCopyToPasteboard,

                                              UIActivityTypeAssignToContact,
                                              UIActivityTypeMail,
                                              UIActivityTypePrint

                                              ];

    }
    else {

        activityVC.excludedActivityTypes = @[ UIActivityTypePostToWeibo,

                                              UIActivityTypeCopyToPasteboard,

                                              UIActivityTypeAssignToContact,
                                              UIActivityTypeMail,
                                              UIActivityTypePrint,
                                              UIActivityTypeAirDrop
                                              ];

    }

    NSLog(@"Test");
       [self.myViewController presentViewController: activityVC animated: YES completion: nil];

   }

在BlogView.m中

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
                        object:(PFObject *)object
{
    static NSString *CellIdentifier = @"Cell";

    Cell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[Cell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    self.theObject = object;

    // Configure the cell to show todo item with a priority at the bottom
    cell.profileName.text = object[@"Title"];
    cell.contentLabel.text = object[@"Request"];
    cell.firstName = object[@"FirstName"];
    cell.lastName = object[@"LastName"];
    cell.iostoken = object[@"DeviceID"];
    cell.request = object[@"Title"];
    cell.prayerObject = object;
    PFFile *thumbnail = object[@"ProfilePic"];
    cell.profilePic.image = [UIImage imageNamed:@"AppIcon60x60@2x.png"];
    [thumbnail getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) {

        UIImage *thumbnailImage = [UIImage imageWithData:imageData];
        UIImageView *thumbnailImageView = [[UIImageView alloc] initWithImage:thumbnailImage];

        cell.profilePic.image = thumbnailImage;

    }];
    NSString *dates = object[@"dateMade"];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"MMM_dd_yyyy"];
   NSDate *datefromstring = [formatter dateFromString:dates];
    NSDateFormatter *formatter2 = [[NSDateFormatter alloc] init];
    [formatter2 setDateFormat:@"MMM dd, yyyy"];
    cell.dateLabel.text = [formatter2 stringFromDate:datefromstring];
    UIFont *cellFont = [UIFont fontWithName:@"Verdana-Bold" size:15];
    UIFont *cellFont2 = [UIFont fontWithName:@"Verdana-Bold" size:12];



    return cell;
}

我没有收到任何警告或错误,但是当我点击按钮时,没有任何反应。

1 个答案:

答案 0 :(得分:2)

第一步:从您单元格上的按钮获取一个IBOutlet到CustomCell.h文件......

@property (weak, nonatomic) IBOutlet UIButton *commentButton;

第二步:在cellForRowAtIndexPath中,为每个单元格上的按钮添加一个选择器并按照您想要的方式装扮...... 注意我们正在添加选择器到每个单元格按钮&#39;现在......不是self.commentButton

[cell.commentButton addTarget:self action:@selector(didTapCommentButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[cell.commentButton setTitle:@"Share" forState:UIControlStateNormal];
[cell.commentButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[cell.commentButton setTitleColor:[UIColor greenColor] forState:UIControlStateHighlighted];

第三步:在BlogView.m文件中实现选择器方法,而不是在CustomCell.m内部实现