这是我的表格视图,
最初,它看起来像这样,当我点击这个单元格然后它将展开
我的问题:我向UITableviewCell添加了一个按钮并为其提供了操作。当我点击附件按钮时,它必须导航到下一个视图。我尝试了下面的代码,但它适用于UIViewcontroller,而不适用于UITableViewCell。
AttachmentView *attach=[self.storyboard instantiateViewControllerWithIdentifier:@"attachViewId"];
[self.navigationController pushViewController:attach animated:YES];
如何实现这一目标?
这是我的ConversationTableViewCell.h文件,
#import <UIKit/UIKit.h>
@interface ConversationTableViewCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UIImageView *profilePicView;
@property (weak, nonatomic) IBOutlet UILabel *clientNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *internalNoteLabel;
@property (weak, nonatomic) IBOutlet UILabel *timeStampLabel;
-(void)setUserProfileimage:(NSString*)imageUrl;
@property (weak, nonatomic) IBOutlet UIWebView *webView;
@property (weak, nonatomic) IBOutlet UIView *view1;
- (IBAction)clickedOnAttachment:(id)sender;
这是ConversationTableViewCell.m文件代码,
#import "HexColors.h"
#import "ConversationTableViewCell.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "AttachmentViewController.h"
@implementation ConversationTableViewCell
- (void)awakeFromNib {
[super awakeFromNib];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
}
-(void)setUserProfileimage:(NSString*)imageUrl
{
// self.profilePicView.layer.borderWidth=1.25f;
self.profilePicView.layer.borderColor=[[UIColor hx_colorWithHexRGBAString:@"#0288D1"] CGColor];
[self.profilePicView sd_setImageWithURL:[NSURL URLWithString:imageUrl]
placeholderImage:[UIImage imageNamed:@"default_pic.png"]];
}
- (IBAction)clickedOnAttachment:(id)sender {
// NSLog(@"CLciked...!");
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main"
bundle: nil];
AttachmentViewController * attach=[mainStoryboard instantiateViewControllerWithIdentifier:@"attachId"];
[self.navigation];
}
@end
这是视图控制器代码,
// this is ConversationTableViewCell view controllers .m file
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
ConversationTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"ConvTableViewCell"];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ConversationTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
NSDictionary *finaldic=[mutableArray objectAtIndex:indexPath.row];
@try{
attachmentArray=[finaldic objectForKey:@"attach"];
if ([attachmentArray count] != 0){
NSIndexPath *path;
NSDictionary *attachDictionary=[attachmentArray objectAtIndex:path.row];
// NSLog(@"Attchment Dict is: %@",attachDictionary);
NSString *numStr = [NSString stringWithFormat:@"%@", [attachDictionary objectForKey:@"file"]];
}else
{
NSLog(@"EMpty aaray");
}
cell.timeStampLabel.text=[utils getLocalDateTimeFromUTC:[finaldic objectForKey:@"created_at"]];
NSString *body= [finaldic objectForKey:@"body"];
NSRange range = [body rangeOfString:@"<body"];
if(range.location != NSNotFound) {
// Adjust style for mobile
float inset = 40;
NSString *style = [NSString stringWithFormat:@"<style>div {max-width: %fpx;}</style>", self.view.bounds.size.width - inset];
body = [NSString stringWithFormat:@"%@%@%@", [body substringToIndex:range.location], style, [body substringFromIndex:range.location]];
}
cell.webView.translatesAutoresizingMaskIntoConstraints = NO;
[cell.webView loadHTMLString:body baseURL:nil];
//NSString *system= @"System";
NSString *fName=[finaldic objectForKey:@"first_name"];
NSString *lName=[finaldic objectForKey:@"last_name"];
NSString *userName=[finaldic objectForKey:@"user_name"];
[Utils isEmpty:fName];
[Utils isEmpty:lName];
[Utils isEmpty:userName];
if ([Utils isEmpty:fName] && [Utils isEmpty:lName]){
if(![Utils isEmpty:userName]){
userName=[NSString stringWithFormat:@"%@",[finaldic objectForKey:@"user_name"]];
cell.clientNameLabel.text=userName;
}else cell.clientNameLabel.text=@"System";
}
else if ((![Utils isEmpty:fName] || ![Utils isEmpty:lName]) || (![Utils isEmpty:fName] && ![Utils isEmpty:lName]))
{
NSString * fName12=[NSString stringWithFormat:@"%@ %@",fName,lName];
cell.clientNameLabel.text=fName12;
}
// prifile image
if([[finaldic objectForKey:@"profile_pic"] hasSuffix:@"system.png"] || [[finaldic objectForKey:@"profile_pic"] hasSuffix:@".jpg"] || [[finaldic objectForKey:@"profile_pic"] hasSuffix:@".jpeg"] || [[finaldic objectForKey:@"profile_pic"] hasSuffix:@".png"] )
{
[cell setUserProfileimage:[finaldic objectForKey:@"profile_pic"]];
}
else if(![Utils isEmpty:fName])
{
[cell.profilePicView setImageWithString:fName color:nil ];
}
else
{
[cell.profilePicView setImageWithString:userName color:nil ];
}
}@catch (NSException *exception)
{
[utils showAlertWithMessage:exception.name sendViewController:self];
NSLog( @"Name: %@", exception.name);
NSLog( @"Reason: %@", exception.reason );
// return;
}
@finally
{
NSLog( @" I am in cellForROwAtIndexPath method in Conversation ViewController" );
}
return cell;
}
答案 0 :(得分:0)
self.navigationController
在ConversationTableViewCell
课程中不可用。
为了在点击单元格/单元格上的按钮时推送UIViewController
的新实例,您应该使用Delegation design pattern
。
示例 -
//ConversationTableViewCell.h
@protocol ConversationTableViewCellDelegate;
@interface ConversationTableViewCell : UITableViewCell
@property (assign, nonatomic) id < ConversationTableViewCellDelegate > delegate;
@end
@protocol ConversationTableViewCellDelegate <NSObject>
@optional
- (void)buttonTouchedForCell:(ConversationTableViewCell *)cell;
@end
//ConversationTableViewCell.m
@implementation ConversationTableViewCell
@synthesize delegate = _delegate;
- (IBAction)clickedOnAttachment:(id)sender {
if ([self.delegate respondsToSelector:@selector(buttonTouchedForCell:)])
[self.delegate buttonTouchedForCell:self];
}
@end
@interface YourController : UIViewController <ConversationTableViewCellDelegate>
@end
@implementation YourController
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
ConversationTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"ConvTableViewCell"];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ConversationTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
[cell setDelegate:self];
// cell related code
return cell;
}
- (void) buttonTouchedForCell:(ConversationTableViewCell *)cell {
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
AttachmentView *attach=[self.storyboard instantiateViewControllerWithIdentifier:@"attachViewId"];
[self.navigationController pushViewController:attach animated:YES];
}
@end