Je développe une application (par un tutoriel, mais le tutoriel est écrit pour Swift et j'apprends Objective-C. Tutoriel sourceiOS vues personnalisées ne montrent pas
Mais j'ai un problème, Je développe un contrôleur personnalisé (RatingController), qui fonctionne lorsque j'utilise une seule de ce contrôleur personnalisé dans une scène La source de commande personnalisée est ici.
@implementation RatingController
//MARK: Properties
const int spacing = 5;
const int starCount = 5;
UIButton *ratingButtons[starCount];
-(void)setRating:(int)rating{
_rating = rating;
[self setNeedsLayout];
}
-(int)getRating{
return _rating;
}
//MARK: Initialization
- (instancetype)initWithCoder:(NSCoder *)coder
{
self = [super initWithCoder:coder];
if (self) {
//load the images
UIImage *filledStarImage = [UIImage imageNamed:@"filledStar"];
UIImage *emptyStarImage = [UIImage imageNamed:@"emptyStar"];
for (int i = 0; i < starCount; i++) {
//create button
UIButton *button = [[UIButton alloc]init];
//set the images
[button setImage:emptyStarImage forState:UIControlStateNormal];
[button setImage:filledStarImage forState:UIControlStateSelected];
[button setImage:filledStarImage forState:UIControlStateHighlighted|UIControlStateSelected];
//No additional highlight when switching state
button.adjustsImageWhenHighlighted = false;
//hook up the method which should be executed when the user touches a button
[button addTarget:self action:@selector(ratingButtonTapped:) forControlEvents:UIControlEventTouchDown];
//add the button the the ratingButton array
ratingButtons[i] = button;
//add the button to the view
[self addSubview:button];
}
}
return self;
}
-(void) layoutSubviews{
//Set the button's width and height to the height of the frame.
const int buttonSize = self.frame.size.height;
CGRect buttonFrame = CGRectMake(0, 0, buttonSize, buttonSize);
for(int i = 0; i < starCount; i++){
buttonFrame.origin.x = (i * (buttonSize + spacing));
ratingButtons[i].frame = buttonFrame;
}
[self updateButtonSelectionState];
}
-(CGSize) intrinsicContentSize{
const int buttonSize = self.frame.size.height;
const int width = (buttonSize * starCount) + (spacing * (starCount -1));
return CGSizeMake(width, buttonSize);
}
//MARK: Button Action
-(void) updateButtonSelectionState{
for(int i = 0; i < starCount; i++)
ratingButtons[i].selected = i < _rating;
}
-(void) ratingButtonTapped:(UIButton *) button{
for(int i = 0; i < starCount; i++){
if(ratingButtons[i] == button){
_rating = i + 1;
}
}
[self updateButtonSelectionState];
}
@end
quand je mets 2 de ces contrôleurs personnalisés dans 1 scène je ne peux voir que celle qui est ajoutée à la fin (dans une liste seulement le dernier élément de liste montre le contrôleur personnalisé) J'espère que quelqu'un peut aider m e.
Merci beaucoup!
Qu'entendez-vous par deux contrôleurs dans une scène? Les utilisez-vous dans des vues de conteneurs? –
Comme ici: https://snag.gy/n0jK1h.jpg chaque élément de la liste doit contenir les étoiles –
Pourriez-vous poster le code de votre source de données tableview, par exemple. cellForRowAtIndexPath. – JingJingTao