2013-12-24 5 views
0

У меня есть проект, который отображает фид статусов, подобных другим социальным сетям. В каждом элементе подачи есть несколько кнопок, которые делают разные вещи. 1 из кнопок открывает новый диспетчер представлений, который отображает комментарии, которые были опубликованы в определенном состоянии. Когда эта кнопка нажата и контроллер открывается, я бы хотел, чтобы она была нажатием на кнопку, чтобы их кнопка назад, и пользователь может вернуться к фиду.Добавьте кнопку push на кнопку внутри пользовательской ячейки таблицы.

Когда эта кнопка нажата и запускается новый vc, некоторые уникальные данные о конкретном статусе/ячейке, которую нужно щелкнуть, необходимо отправить в «Комментарии vc». Где будет код для этого?

CUSTOM CELL .Н

#import <UIKit/UIKit.h> 

@interface FeedItemCell : UITableViewCell 
@property (weak, nonatomic) IBOutlet UIImageView *DefaultImg; 
@property (weak, nonatomic) IBOutlet UILabel *NameLabel; 
@property (weak, nonatomic) IBOutlet UILabel *StatusLabel; 
@property (weak, nonatomic) IBOutlet UILabel *timeLabel; 


@property (nonatomic, copy) NSString *msg_id; 
@property (nonatomic, copy) NSString *status; 
@property (nonatomic, weak) IBOutlet UIButton* commentButton; 
@property (nonatomic, weak) IBOutlet UIButton* bumpButton; 
@property (strong, nonatomic) id delegate; 
-(IBAction)viewComments:(id)sender; 
-(IBAction)bump:(id)sender; 

@end 


@protocol CustomCellProtocol <NSObject> 
- (void)EBCellPressed:(NSString *)cellName; 

CUSTOM CELL .М

#import "FeedItemCell.h" 
#import "CommentsViewController.h" 
#import "NSDate+TimeAgo.h" 


@interface FeedItemCell() <WYPopoverControllerDelegate> 
{ 


} 
- (IBAction)open:(id)sender; 
- (void)close:(id)sender; 
@end 


@implementation FeedItemCell 
@synthesize commentButton; 
- (instancetype)initWithDelegate:(id)delegate { 
    self = [super init]; 
    if (self) { 
     self.delegate = delegate; 
     // Initialization code 
    } 
    return self; 
} 

-(IBAction)bump:(id)sender{ 

    [self.delegate EBCellPressed:@"NAME"]; 



} 

- (IBAction)open:(id)sender 
{ 

} 








@end 

publicFeed. M

#import "PublicFeedViewController.h" 
#import "FeedItemCell.h" 
#import "AFNetworking.h" 
#import "UIImageView+WebCache.h" 
#import "InboxDetailViewController.h" 
#import "SWRevealViewController.h" 
#import "CommentsViewController.h" 
#import "NSDate+TimeAgo.h" 


@interface PublicFeedViewController(){ 
    NSArray *NameLabel; 
    NSArray *StatusLabel; 
    NSMutableArray *feedArray; 
} 

@end 

@implementation PublicFeedViewController 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    //The below code prompts the user for push notifications. If allowed, code in AppDelegate takes over and stores the token. 
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)]; 
    // Do any additional setup after loading the view. 
    self.FeedTable.dataSource=self; 
    self.FeedTable.delegate=self; 

    // Set the side bar button action. When it's tapped, it'll show up the sidebar. 
    _sidebarButton.target = self.revealViewController; 
    _sidebarButton.action = @selector(revealToggle:); 

    // Set the gesture 
    [self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer]; 
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 
    NSDictionary *parameters = @{@"foo": @"bar"}; 
    [UIApplication sharedApplication].networkActivityIndicatorVisible = TRUE; 
    [manager POST:@"www" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) { 
     //NSLog(@"JSON: %@", responseObject); 

     self->feedArray = [responseObject objectForKey:@"feed"]; 

     [self.FeedTable reloadData]; 
     [UIApplication sharedApplication].networkActivityIndicatorVisible = FALSE; 
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
     NSLog(@"Error: %@", error); 
    }]; 


} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ 
    return 1; 
} 
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
    return feedArray.count; 
} 
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 


    NSString *[email protected]"Cell"; 

    FeedItemCell *Cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if(!Cell){ 
     Cell = [[FeedItemCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 

    } 


    NSLog(@"FEED ARRAY: %@", self->feedArray); 
    NSDictionary *tempDictionary= [self->feedArray objectAtIndex:indexPath.row]; 
    // Display recipe in the table cell 
    NSString *thumb_img = [tempDictionary objectForKey:@"thumb_img"]; 
    NSString *[email protected]"http://buhzhyve.com/CI_REST_LOGIN/UPLOADS/thumbs/"; 
    NSString *thumb_url = [thumb_path stringByAppendingString:thumb_img]; 



    Cell.NameLabel.text=[tempDictionary objectForKey:@"first_name"]; 
    Cell.StatusLabel.text=[tempDictionary objectForKey:@"message"]; 
    Cell.msg_id=[tempDictionary objectForKey:@"msg_id"]; 
    //Cell.status=[tempDictionary objectForKey:@"message"]; 
    Cell.StatusLabel.lineBreakMode=0; 
    Cell.StatusLabel.numberOfLines=0; 
    NSString *commentCount = [tempDictionary objectForKey:@"comment_count"]; 
    NSString *commentButtonText =[NSString stringWithFormat:@"Comments (%@)",commentCount]; 
    [Cell.commentButton setTitle:commentButtonText forState: UIControlStateNormal]; 
    NSString *bumpCount = [tempDictionary objectForKey:@"bump_count"]; 
    NSString *bumpButtonText =[NSString stringWithFormat:@"Bumps (%@)",bumpCount]; 
    [Cell.bumpButton setTitle:bumpButtonText forState: UIControlStateNormal]; 
    //[Cell.StatusLabel sizeToFit]; 
    NSString *created_string=[tempDictionary objectForKey:@"created"]; 
    double created_double = created_string.doubleValue; 
    NSDate *date = [[NSDate alloc] initWithTimeIntervalSince1970:created_double]; 
    NSString *ago = [date timeAgo]; 
    Cell.timeLabel.text=ago; 




    //Cell.DefaultImg.image = [UIImage imageNamed:@"buhz_mini_logo.png"]; 

    [Cell.DefaultImg setImageWithURL:[NSURL URLWithString:thumb_url] 
        placeholderImage:[UIImage imageNamed:@"buhz_mini_logo.png"]]; 
    return Cell; 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    //Ideally you should do lazy loading so that instead of creating a new textView each time, you just reuse the same one. 
    UITextView *temp = [[UITextView alloc] initWithFrame:CGRectMake(82, 26, self.FeedTable.frame.size.width, 18)]; //This initial size doesn't matter 
    NSDictionary *tempDictionary= [self->feedArray objectAtIndex:indexPath.row]; 
    NSString *status = [tempDictionary objectForKey:@"message"]; 
    temp.font =[UIFont fontWithName:@"System" size:12]; 
    temp.text = status; 


    CGFloat textViewWidth = 218; 
    CGRect tempFrame = CGRectMake(82,26,textViewWidth,18); //The height of this frame doesn't matter. 
    CGSize tvsize = [temp sizeThatFits:CGSizeMake(tempFrame.size.width, tempFrame.size.height)]; //This calculates the necessary size so that all the text fits in the necessary width. 

    //Add the height of the other UI elements inside your cell 

    return tvsize.height + 70; 


} 

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

    } 
} 

@end 

publicfeed .h

#import <UIKit/UIKit.h> 

@interface PublicFeedViewController : UIViewController<UITableViewDataSource,UITableViewDelegate> 
@property (weak, nonatomic) IBOutlet UITableView *FeedTable; 
@property (weak, nonatomic) IBOutlet UIBarButtonItem *sidebarButton; 
- (IBAction)addItem; 



@end 

ответ

1

Так если предположить, что вы создаете эту кнопку в коде, это то, как вы могли бы справиться с этим.

Эта первая строка сообщает кнопке, что при ее нажатии ей необходимо вызвать этот конкретный селектор/метод, отправленный как действие.

[button addTarget:self action:@selector(showNextViewController) forControlEvents:UIControlEventTouchUpInside]; 

Тогда вы должны создать этот метод в том же классе.

- (void) showNextViewController 
{ 
    NewViewController *newViewController = [[NewViewController alloc] init]; //Edit this line of course to fit for your situation. I'm not sure if you're loading from an XIB or from a Storyboard, or neither. 
    newViewController.someVariable = someVariable; 
    newViewController.someOtherVariable = someOtherVariable; 

    [[[[[UIApplication sharedApplication] delegate] window] rootViewController].navigationController pushViewController:view animated:YES]; 
} 

Это будет посылать необходимые данные для нового контроллера представления, и он будет также отображать новый взгляд на экран с помощью кнопки возврата.

Надеюсь, что это сработает!

+0

так inisde моего пользовательского права ячейки? – ChuckKelly

+0

Да, внутри вашей пользовательской ячейки. – adeiji

+0

Cant access navigation controller изнутри пользовательской ячейки, поэтому я сделал некоторые поиски и наткнулся на это: http://iphonedevsdk.com/forum/iphone-sdk-development/42902-call-pushviewcontroller-from-a-custom-uitableviewcell. html но, это не работает ни bc Мне нужно отправить msg_id из конкретной ячейки, на которую было нажата кнопка commentviewcontroller, и используя способ, описанный в этой ссылке, я не вижу средства для этого. – ChuckKelly

0

1. Хорошо, давайте сделаем вид, что это ваш собственный класс ячеек. в вашем .h файле пользовательской ячейки, вам необходимо добавить протокол.

#import <UIKit/UIKit.h> 

@interface CustomCell : UIView 
@property (strong, nonatomic) id delegate; //this is used for sending messages out of the custom cell 

//init 
- (id)initWithFrame:(CGRect)frame andCatName:(NSString *)name andDelegate:(id)delegate; 

@end 
@protocol CustomCellProtocol <NSObject> 

-(void)customCellSelected:(NSString *)cellName; 

@end 

, что мы на самом деле это что-то вроде создания пользовательского события, что класс может выплеснуть и каждый, кто подписывается на который может запустить метод, когда customCellSelected отбрасывается.

2. Теперь при создании каждой пользовательской ячейки с помощью метода init вам необходимо предоставить делегат, какой вид точек, к какому классу следует перенос пользовательских ячейки вызов customCellSelected так в методе инициализации вы установили этот делегат.

- (id)initWithFrame:(CGRect)frame andDelegate:(id)delegate { 
    self = [super initWithFrame:frame]; 
    if (self) { 
     self.delegate = delegate; //setting which class should be called when calling protocol methods. 
     // Your initialization code 
    } 
    return self; 
} 

3. Теперь в вашем.м файл пользовательской ячейки, когда пользователь нажимает свою кнопку и вы вводите свой метод, пусть это будет buttonPressed

- (IBAction) buttonPressed:(id)sender { 
    [self.delegate customCellSelected:@"THE CELL'S NAME"]; // calling the protocol method. 
} 

теперь вызов метода делегата должен быть переведен в ОМ, потому что при создании пользовательского ячейку вы используете initWithFrame:(CGRect)frame andDelegate:(id)delegate, и вы передаете self в качестве делегата, поэтому, когда вызывается [self.delegate customCellSelected:@"THE CELL'S NAME"];, он фактически вызывается на vc.

4. это, как вы создаете пользовательские ячейки в ОМ:

customCell *tempView = [[customCell alloc] initWithFrame:CGRectMake(X, Y, Width, Height) andDelegate:self]; // here you set the vc as the delegate 

ого Теперь все, что вам нужно сделать, это добавить метод customCellSelected к коду В.- так что он может назвать когда пользовательский Cell вызывает его.

- (void)customCellSelected:(NSString *)cellName { 
    self.selectedCell = cellName; 
    [self performSegueWithIdentifier:@"SelectedCell" sender:self]; 
} 

6.then добавить это в ВХ:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
    if ([segue.identifier isEqualToString:@"SelectedCell"]) { 
     LevelSelectViewController *levelSelectViewController = (LevelSelectViewController *)segue.destinationViewController; 
     levelSelectViewController.cellName = self.selectedCell; 
    } 
} 

7.only вещь, которую вы должны помнить о том, чтобы создать переход от первого Биотех второй, как это:

how to create segue

+0

и как именно я могу позвонить: [self performSegueWithIdentifier: @ "segue1" отправитель: self]; из пользовательского класса ячеек? – ChuckKelly

+0

@ChuckKelly вы можете либо инициализировать каждую ячейку с помощью экземпляра 'self', либо использовать протокол и делегирование, а также вызывать, например,' cellPressedCall', вашему viewController и затем вызывать его. Я добавил код для делегации. –

+0

@ChuckKelly Я добавил больше кода для вас. –

Смежные вопросы