-1

I am new in iOS and I am facing problem regarding to get the value from custom table view cell to view controller. I am using rate view for rating and I am checking if value of rate is less then 3 then it show have to enter text in the text view and I want to get value in view controller My code is like this

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;
        }

    }

How can I get label and textview value in viewcontroller and I want to set rate view value to rate view after reload table.

enter image description here

enter image description here

Hear in the Image i am getting five star and If I click on less then 3 star it should have to write comment.I am taking rate value in the label.How can I get both label and textvalue in view controller.Please tell me to update question if you want more data.Thanks in Advance!

Muju
  • 884
  • 20
  • 54
  • check this ...http://stackoverflow.com/questions/16100378/is-there-any-controls-available-for-star-rating – Sudheer Kolasani Jan 10 '17 at 09:46
  • @SudheerKolasani Rating is work perfect My issue is I am not able to get its value to view controller fro custom table view. – Muju Jan 10 '17 at 09:48

1 Answers1

1

So write this in your CustomTableViewCell.h

@property(nonatomic,assign)NSInteger selectedRating;

everytime when the rating is updated in the Cell, you have to change the value of this variable.

In the viewController it depends on when you want to get the value of this cell. For example in this method:

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
       CustomTableViewCell *yourCell = (CustomerTableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
    NSLog(@"%lu",yourCell.selectedRating);
}

Let me know if this was helpful! Another way without a variable is to read the NSString in the statusLabel and change the string to a NSInteger variable...

If you are using your delegate...

Then implement the delegate RateViewDelegatein the viewController. The delegate is called in the class to which it is assigned...

Try this

You have to add the @property (nonatomic, weak) NSObject<RateViewDelegate>* delegate; to the header file of your CustomTableViewCell.h in the method

-(UITableViewCell) cellForRowAtIndexPath....

you have to assign the delegate like this

cell.delegate = self;

Now modify your cell delegate method to this

- (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];
   }
}
Muju
  • 884
  • 20
  • 54
Gulliva
  • 488
  • 2
  • 10
  • Do not want to use didSelectRowAtIndexPath and button click. – Muju Jan 10 '17 at 09:57
  • Which method do you want to use? – Gulliva Jan 10 '17 at 09:57
  • I am getting value in - (void)rateView:(RateView *)rateView ratingDidChange:(int)rating this method and I want to pass its value to view controller. – Muju Jan 10 '17 at 09:59
  • So everytime when this method is called? You should think about a delegate which is implemented in your viewcontroller. Everytime the method `- (void)rateView:(RateView *)rateView ratingDidChange:(int)rating` is called you should call the delegate. But if you do it this way, you have to set the delegate in each cell. I think this is not best practice... – Gulliva Jan 10 '17 at 10:01
  • I am setting this delegate - (void)rateView:(RateView *)rateView ratingDidChange:(int)rating in custom table view cell and I am getting value in it but not in the view controller. – Muju Jan 10 '17 at 10:09
  • Showing error if(self.delegate respondsToSelector:@selector(rateView:ratingDidChange:){ [self.delegate ratingView:rateView ratingDidChange:rating]; } at this line expected ]. – Muju Jan 10 '17 at 10:28
  • Syntax error by me, I wrote it without xcode. You have to try yourself a little bit if copy&paste doesn't work perfectly... – Gulliva Jan 10 '17 at 10:30
  • Showing error No known method for selector respondsToSelector. – Muju Jan 10 '17 at 10:32
  • So you added the delegate like this `@property(nonatomic,assign)id delegate;` to the cell.h file? Retry this: `if([self.delegate respondsToSelector:@selector(rateView:ratingDidChange:)]){ [self.delegate rateView:rateView ratingDidChange:rating]; }` – Gulliva Jan 10 '17 at 10:36
  • Sorry but without your project I can't do the whole work for you... So the repsondsToSelector method checks if the given delegate implements the method rateView:ratingDidChange: ... If the delegate don't do this, your app will crash with "Is not responding to selector" exception... This is the common way to implement a delegate... Type the if column yourself like I did. Normally codecompletition will help you to select the right method... – Gulliva Jan 10 '17 at 10:42
  • @Muju where is your problem localized ? Do you understand my approach? – Gulliva Jan 10 '17 at 11:40
  • at this line respondsToSelector. – Muju Jan 10 '17 at 11:41
  • @Muju I can't understand why! Put this line in a comment and try it without... Else show me more code! – Gulliva Jan 10 '17 at 11:42
  • then nothing is happen. – Muju Jan 10 '17 at 11:43
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/132786/discussion-between-gulliva-and-muju). – Gulliva Jan 10 '17 at 11:43