从 UITableviewCell 上的按钮导航到其他视图控制器的代码是啥?
Posted
技术标签:
【中文标题】从 UITableviewCell 上的按钮导航到其他视图控制器的代码是啥?【英文标题】:What will be the code for navigating from button on UITableviewCell to other view controller?从 UITableviewCell 上的按钮导航到其他视图控制器的代码是什么? 【发布时间】:2018-03-26 07:22:26 【问题描述】:这是我的表格视图,
最初是这样的,当我点击这个单元格时,它会展开
我的问题:我在 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;
【问题讨论】:
你写过[self.navigationController attach animated:YES];
或[self.navigationController pushViewController: attach animated:YES];
对不起,我忘了。 pushViewController 没有被复制。是的,我试过它给出了一个错误,即在 ConversationTableViewCell 上找不到 self.view 或 self.navigationController。
您是否在自定义单元子类中编写了该代码?
是的,我已经这样做了
请发布您的cellforrowatindexpath
代码。
【参考方案1】:
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
【讨论】:
让我检查一下@sanjay 确定@Nikita,一切顺利!以上是关于从 UITableviewCell 上的按钮导航到其他视图控制器的代码是啥?的主要内容,如果未能解决你的问题,请参考以下文章
从 UIAlertController 按钮单击导航到 ViewController
UITableViewCell 上的动画按钮而不影响其他 UITableViewCell