ich eine App zu entwickeln (durch ein Tutorial, aber das Tutorial ist für Swift geschrieben und ich lerne Objective-C. Tutorial sourceiOS Benutzerdefinierte Ansichten zeigen nach oben nicht
Aber ich habe ein Problem, ich bin eine benutzerdefinierte Controller (RatingController) zu entwickeln, das funktioniert, wenn ich nur in einer Szene eine dieser benutzerdefinierten Controller verwenden Die Custom-Controller Quelle ist hier:.
@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
wenn ich 2 in 1 dieser benutzerdefinierten Controller setzen Szene kann ich nur die sehen, die am Ende hinzugefügt wird (in einer Liste nur das letzte Listenelement zeigt den benutzerdefinierten Controller) Ich hoffe jemand kann helfen m e.
Vielen Dank schon!
Was meinen Sie mit zwei Controllern in einer Szene? Verwenden Sie sie in Containeransichten? –
Wie hier: https://snag.gy/n0jK1h.jpg jedes Listenelement sollte die Sterne enthalten –
Konnten Sie den Code für Ihre tableview Datenquelle, z. cellForRowAtIndexPath. – JingJingTao