我是iOS的新手,我遇到的问题是从自定义表格视图单元格中获取值以查看控制器。我正在使用费率视图进行评级,我正在检查费率的值是否小于3然后它显示必须在文本视图中输入文本,我想在视图控制器中获取值 我的代码就像这样
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.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;
}
}
如何在viewcontroller中获取label和textview值,我想在重新加载表后设置rate view value to rate view。
在图像中听到我得到五颗星,如果我点击少于3颗星就应该写评论。我在标签中取价值。如何在视图控制器中同时获得标签和文本值。请如果你想要更多数据,请告诉我更新问题。谢谢你提前!
答案 0 :(得分:1)
所以在CustomTableViewCell.h中写这个
@property(nonatomic,assign)NSInteger selectedRating;
每次在Cell中更新评级时,您都必须更改此变量的值。
在viewController中,它取决于您何时想要获取此单元格的值。例如,在这种方法中:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
CustomTableViewCell *yourCell = (CustomerTableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
NSLog(@"%lu",yourCell.selectedRating);
}
如果这有用,请告诉我! 没有变量的另一种方法是读取statusLabel中的NSString并将字符串更改为NSInteger变量...
如果您正在使用您的代表......
然后在viewController中实现委托RateViewDelegate
。委托在分配给它的类中调用...
试试这个
您必须添加@property (nonatomic, weak) NSObject<RateViewDelegate>* delegate;
到方法
-(UITableViewCell) cellForRowAtIndexPath....
你必须像这样分配代表
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];
}
}