Ios 如何在不使用按钮的情况下在视图控制器上获取自定义TableViewCell值,并在目标c中重新加载表格后进行设置
我是iOS新手,我面临着从自定义表视图单元格到视图控制器获取值的问题。我正在使用rate视图进行评级,我正在检查rate的值是否小于3,然后显示必须在文本视图中输入文本,我希望在视图控制器中获取值 我的代码是这样的Ios 如何在不使用按钮的情况下在视图控制器上获取自定义TableViewCell值,并在目标c中重新加载表格后进行设置,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我是iOS新手,我面临着从自定义表视图单元格到视图控制器获取值的问题。我正在使用rate视图进行评级,我正在检查rate的值是否小于3,然后显示必须在文本视图中输入文本,我希望在视图控制器中获取值 我的代码是这样的 CustomTableviewcell.h @interface NextTableview : UITableViewCell<RateViewDelegate,UITextViewDelegate> { NSString *StatusValue;
CustomTableviewcell.h
@interface NextTableview : UITableViewCell<RateViewDelegate,UITextViewDelegate>
{
NSString *StatusValue;
UILabel *lbl;
}
@property(nonatomic,strong) IBOutlet UILabel *staticlbl;
@property(nonatomic,strong) IBOutlet UITextView *commenttxtview;
@property(nonatomic,strong) IBOutlet UILabel *Kpiidlbl;
@property (weak, nonatomic) IBOutlet RateView *rateView;
@property (weak, nonatomic) IBOutlet UILabel *statusLabel;
@end
CustomTableviewcell.h
@接口NextTableview:UITableViewCell
{
NSString*状态值;
UILabel*lbl;
}
@属性(非原子,强)IBUILabel*staticlbl;
@属性(非原子,强)IBOutlet UITextView*commenttxtview;
@属性(非原子,强)IBUILabel*Kpiidlbl;
@属性(弱、非原子)IBOutlet RateView*RateView;
@属性(弱、非原子)IBUILabel*statusLabel;
@结束
CustomTableviewcell.m
@synthesize rateView,staticlbl,statusLabel,commenttxtview,Kpiidlbl;
- (void)awakeFromNib {
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
commenttxtview.layer.borderWidth = 0.70f;
commenttxtview.layer.borderColor = [[UIColor blackColor] CGColor];
commenttxtview.delegate=self;
UIToolbar* doneToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
doneToolbar.barStyle = UIBarStyleBlackTranslucent;
doneToolbar.items = [NSArray arrayWithObjects:
[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(doneButtonClickedDismissKeyboard)],
nil];
[doneToolbar sizeToFit];
commenttxtview.inputAccessoryView = doneToolbar;
lbl = [[UILabel alloc] initWithFrame:CGRectMake(10.0, 0.0,90.0, 34.0)];
[lbl setText:@"Enter Text"];
[lbl setFont:[UIFont systemFontOfSize:12]];
[lbl setBackgroundColor:[UIColor clearColor]];
[lbl setTextColor:[UIColor lightGrayColor]];
commenttxtview.delegate = self;
[commenttxtview addSubview:lbl];
statusLabel.hidden=YES;
commenttxtview.hidden=YES;
// Do any additional setup after loading the view from its nib.
self.rateView.notSelectedImage = [UIImage imageNamed:@"not_selected_star@2x.png"];
self.rateView.halfSelectedImage = [UIImage imageNamed:@"half_selected_star@2x.png"];
self.rateView.fullSelectedImage = [UIImage imageNamed:@"selected_star@2x.png"];
self.rateView.rating = 0;
self.rateView.editable = YES;
self.rateView.maxRating = 5;
self.rateView.delegate = self;
Kpiidlbl.hidden=YES;
}
-(void)doneButtonClickedDismissKeyboard
{
[commenttxtview resignFirstResponder];
// commenttxtview.hidden=YES;
}
- (void)textViewDidEndEditing:(UITextView *)theTextView
{
if (![commenttxtview hasText]) {
lbl.hidden = NO;
}
}
- (void) textViewDidChange:(UITextView *)textView
{
if(![commenttxtview hasText]) {
lbl.hidden = NO;
}
else{
lbl.hidden = YES;
}
}
- (void)rateView:(RateView *)rateView ratingDidChange:(int)rating {
self.statusLabel.text = [NSString stringWithFormat:@"%d", rating];
NSLog(@"Rating value =%@",self.statusLabel.text);
StatusValue=statusLabel.text;
NSLog(@"Status Value String =%@",StatusValue);
// Hear I am getting value of rating..in StatusValue..
int status=[StatusValue intValue];
if(status<=3)
{
commenttxtview.hidden=NO;
}
else{
commenttxtview.hidden=YES;
}
}
@synthesis rateView、staticlbl、statusLabel、commenttxtview、Kpiidlbl;
-(无效)从NIB中唤醒{
//初始化代码
}
-(无效)设置选定:(BOOL)选定动画:(BOOL)动画{
[超级设置选定:选定动画:动画];
//为所选状态配置视图
commenttxtview.layer.borderWidth=0.70f;
commenttxtview.layer.borderColor=[[UIColor blackColor]CGColor];
commenttxtview.delegate=self;
UIToolbar*doneToolbar=[[UIToolbar alloc]initWithFrame:CGRectMake(0,0320,50)];
doneToolbar.barStyle=UIBarStyleBlack半透明;
doneToolbar.items=[NSArray arrayWithObjects:
[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarbuttonSystemFlexibleSpace目标:nil操作:nil],
[[UIBarButtonItem alloc]initWithTitle:@“完成”样式:UIBarButtonItemStyleDone目标:自我操作:@selector(doneButtonClickedDismissKeyboard)],
零];
[doneToolbar sizeToFit];
commenttxtview.inputAccessoryView=doneToolbar;
lbl=[[UILabel alloc]initWithFrame:CGRectMake(10.0,0.0,90.0,34.0)];
[lbl setText:@“输入文本”];
[lbl setFont:[UIFont systemFontOfSize:12]];
[lbl setBackgroundColor:[UIColor clearColor]];
[lbl setTextColor:[UIColor lightGrayColor]];
commenttxtview.delegate=self;
[commenttxtview addSubview:lbl];
statusLabel.hidden=是;
commenttxtview.hidden=是;
//从nib加载视图后,执行任何其他设置。
self.rateView.notSelectedImage=[UIImage ImageName:@“未选中”_star@2x.png"];
self.rateView.halfSelectedImage=[UIImage ImageName:@“已选择一半”_star@2x.png"];
self.rateView.fullSelectedImage=[UIImage ImageName:@”已选中_star@2x.png"];
self.rateView.rating=0;
self.rateView.editable=是;
self.rateView.maxRating=5;
self.rateView.delegate=self;
kpiidbl.hidden=是;
}
-(无效)DonebuttonClickedDismiss键盘
{
[commenttxtview辞职FirstResponder];
//commenttxtview.hidden=是;
}
-(无效)文本视图编辑:(UITextView*)文本视图
{
如果(![commenttxtview-hasText]){
lbl.hidden=否;
}
}
-(无效)textViewDidChange:(UITextView*)textView
{
如果(![commenttxtview-hasText]){
lbl.hidden=否;
}
否则{
lbl.hidden=是;
}
}
-(void)rateView:(rateView*)rateView ratingDidChange:(int)rating{
self.statusLabel.text=[NSString stringWithFormat:@“%d”,额定值];
NSLog(@“额定值=%@”,self.statusLabel.text);
StatusValue=statusLabel.text;
NSLog(@“状态值字符串=%@”,状态值);
//听说我正在获得状态值中的评级值。。
int status=[StatusValue intValue];
如果(状态如此,请将其写入CustomTableViewCell.h中
@property(nonatomic,assign)NSInteger selectedRating;
每次在单元格中更新评级时,都必须更改此变量的值
在viewController中,它取决于何时获取此单元格的值。例如,在此方法中:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
CustomTableViewCell *yourCell = (CustomerTableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
NSLog(@"%lu",yourCell.selectedRating);
}
让我知道这是否有用!
没有变量的另一种方法是读取statusLabel中的NSString并将该字符串更改为NSInteger变量
如果您正在使用您的代理
然后在viewController中实现委托RateViewDelegate
试试这个
您必须添加@属性(非原子、弱)NSObject*委托;
方法中CustomTableViewCell.h的头文件
-(UITableViewCell)单元格用于行索引路径…
您必须像这样分配代理
cell.delegate=self;
现在将您的单元格委托方法修改为
- (void)rateView:(RateView *)rateView ratingDidChange:(int)rating {
self.statusLabel.text = [NSString stringWithFormat:@"%d", rating];
NSLog(@"Rating value =%@",self.statusLabel.text);
StatusValue=statusLabel.text;
NSLog(@"Status Value String =%@",StatusValue);
// Hear I am getting value of rating..in StatusValue..
int status=[StatusValue intValue];
if(status<=3)
{
commenttxtview.hidden=NO;
}
else{
commenttxtview.hidden=YES;
}
if([self.delegate respondsToSelector:@selector(rateView:ratingDidChange:)]){
[self.delegate rateView:rateView ratingDidChange:rating];
}
}
-(无效)rateView:(rateView*)rateView ratingDidChange:(int)rating{
self.statusLabel.text=[NSString stringWithFormat:@“%d”,额定值];
NSLog(@“额定值=%@”,self.statusLabel.text);
StatusValue=statusLabel.text;
NSLog(@“状态值字符串=%@”,状态值);
//听说我正在获得状态值中的评级值。。
int status=[StatusValue intValue];
如果(状态检查此…@SudheerKolasani Rating工作正常,我的问题是我无法从自定义表视图中获取其值以查看控制器。不想使用didSelectRowAtIndexPath并单击按钮。您想使用哪种方法?我在-(void)rateView:(rateView*)rateView ratingDidChange:(int)中获取值对该方法进行评级,我希望将其值传递给view controller。因此每次调用该方法时,您应该考虑在viewcontroller中实现的委托。每次该方法-(void)rateView:(rateView*)rateView ratingDidChange:(int)评级
被调用时,您应该调用代理。但是如果这样做,您必须在每个单元格中设置代理。我认为这不是最佳做法…我正在设置