iOS Question Creating Rating Bar

azzam223

Active Member
Licensed User
Hello everyone i found this code in website can any one create b4i library with it

B4X:
#import "ViewController.h"

@interface ViewController ()

@property (strong, nonatomic) IBOutlet UITableViewCell *topMarginCell;
@property (strong, nonatomic) IBOutlet UIButton *star1;
@property (strong, nonatomic) IBOutlet UIButton *star2;
@property (strong, nonatomic) IBOutlet UIButton *star3;
@property (strong, nonatomic) IBOutlet UIButton *star4;
@property (strong, nonatomic) IBOutlet UIButton *star5;

@property (strong, nonatomic) NSArray<UIButton *> *starButtons;

@property (nonatomic) NSInteger rating;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.starButtons = @[ self.star1, self.star2, self.star3, self.star4, self.star5 ];
    self.tableView.backgroundColor = self.topMarginCell.backgroundColor;
}

- (void)reloadDispatchSection {
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationNone];
}

- (IBAction)starButtonWasTapped:(UIButton *)button {
    NSInteger buttonIndex = [self.starButtons indexOfObject:button];
    if (buttonIndex == NSNotFound) { return; }
    self.rating = buttonIndex + 1;

    [self.starButtons enumerateObjectsUsingBlock:^(UIButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj setTitle:(idx <= buttonIndex ? @"★" : @"☆") forState:UIControlStateNormal];
    }];

    [self reloadDispatchSection];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 1 && (self.rating == 0 || self.rating >= 4)) {
        return 0;
    } else {
        return [super tableView:tableView numberOfRowsInSection:section];
    }
}

@end

https://stackoverflow.com/questions...ew-and-labels-inside-uiview/34641167#34641167
 

MarcoRome

Expert
Licensed User
You already have everything inside B4i.
Only a little imagination is needed



The Code:

B4X:
Sub Process_Globals
    'These global variables will be declaBlack once when the application starts.
    'Public variables can be accessed from all modules.
    Public App As Application
    Public NavControl As NavigationController
    Private Page1 As Page

    Private Label1 As Label
    Private Label2 As Label
    Private Label3 As Label
    Private Label4 As Label
    Private Label5 As Label
    Private Label6 As Label
End Sub

Private Sub Application_Start (Nav As NavigationController)
    'SetDebugAutoFlushLogs(True) 'Uncomment if program crashes before all logs are printed.
    NavControl = Nav
    Page1.Initialize("Page1")
    Page1.Title = "Page 1"
    Page1.RootPanel.Color = Colors.White
    Page1.RootPanel.LoadLayout("1")
    NavControl.ShowPage(Page1)
End Sub

Private Sub Page1_Resize(Width As Int, Height As Int)
    
End Sub

Private Sub Application_Background
    
End Sub


Sub Label5_Click
    Dim cs As CSBuilder
    cs.Initialize.Font(Font.CreateFontAwesome(22)).Color(Colors.Black).Append(Chr(0xF005)).PopAll
    Label1.AttributedText = cs
    
    Dim cs2 As CSBuilder
    cs2.Initialize.Font(Font.CreateFontAwesome(22)).Color(Colors.Black).Append(Chr(0xF005)).PopAll
    Label2.AttributedText = cs2
    
    Dim cs3 As CSBuilder
    cs3.Initialize.Font(Font.CreateFontAwesome(22)).Color(Colors.Black).Append(Chr(0xF005)).PopAll
    Label3.AttributedText = cs3

    Dim cs4 As CSBuilder
    cs4.Initialize.Font(Font.CreateFontAwesome(22)).Color(Colors.Black).Append(Chr(0xF005)).PopAll
    Label4.AttributedText = cs4
    
    Dim cs5 As CSBuilder
    cs5.Initialize.Font(Font.CreateFontAwesome(22)).Color(Colors.Black).Append(Chr(0xF005)).PopAll
    Label5.AttributedText = cs5
    Label6.Text = "5 Stars"
End Sub

Sub Label4_Click
    Dim cs As CSBuilder
    cs.Initialize.Font(Font.CreateFontAwesome(22)).Color(Colors.Black).Append(Chr(0xF005)).PopAll
    Label1.AttributedText = cs
    
    Dim cs2 As CSBuilder
    cs2.Initialize.Font(Font.CreateFontAwesome(22)).Color(Colors.Black).Append(Chr(0xF005)).PopAll
    Label2.AttributedText = cs2
    
    Dim cs3 As CSBuilder
    cs3.Initialize.Font(Font.CreateFontAwesome(22)).Color(Colors.Black).Append(Chr(0xF005)).PopAll
    Label3.AttributedText = cs3

    Dim cs4 As CSBuilder
    cs4.Initialize.Font(Font.CreateFontAwesome(22)).Color(Colors.Black).Append(Chr(0xF005)).PopAll
    Label4.AttributedText = cs4
    
    Dim cs5 As CSBuilder
    cs5.Initialize.Font(Font.CreateFontAwesome(22)).Color(Colors.Black).Append(Chr(0xF006)).PopAll
    Label5.AttributedText = cs5
    Label6.Text = "4 Stars"
    
End Sub

Sub Label3_Click
    Dim cs As CSBuilder
    cs.Initialize.Font(Font.CreateFontAwesome(22)).Color(Colors.Black).Append(Chr(0xF005)).PopAll
    Label1.AttributedText = cs
    
    Dim cs2 As CSBuilder
    cs2.Initialize.Font(Font.CreateFontAwesome(22)).Color(Colors.Black).Append(Chr(0xF005)).PopAll
    Label2.AttributedText = cs2
    
    Dim cs3 As CSBuilder
    cs3.Initialize.Font(Font.CreateFontAwesome(22)).Color(Colors.Black).Append(Chr(0xF005)).PopAll
    Label3.AttributedText = cs3

    Dim cs4 As CSBuilder
    cs4.Initialize.Font(Font.CreateFontAwesome(22)).Color(Colors.Black).Append(Chr(0xF006)).PopAll
    Label4.AttributedText = cs4
    
    Dim cs5 As CSBuilder
    cs5.Initialize.Font(Font.CreateFontAwesome(22)).Color(Colors.Black).Append(Chr(0xF006)).PopAll
    Label5.AttributedText = cs5
    Label6.Text = "3 Stars"
    
End Sub

Sub Label2_Click
    Dim cs As CSBuilder
    cs.Initialize.Font(Font.CreateFontAwesome(22)).Color(Colors.Black).Append(Chr(0xF005)).PopAll
    Label1.AttributedText = cs
    
    Dim cs2 As CSBuilder
    cs2.Initialize.Font(Font.CreateFontAwesome(22)).Color(Colors.Black).Append(Chr(0xF005)).PopAll
    Label2.AttributedText = cs2
    
    Dim cs3 As CSBuilder
    cs3.Initialize.Font(Font.CreateFontAwesome(22)).Color(Colors.Black).Append(Chr(0xF006)).PopAll
    Label3.AttributedText = cs3

    Dim cs4 As CSBuilder
    cs4.Initialize.Font(Font.CreateFontAwesome(22)).Color(Colors.Black).Append(Chr(0xF006)).PopAll
    Label4.AttributedText = cs4
    
    Dim cs5 As CSBuilder
    cs5.Initialize.Font(Font.CreateFontAwesome(22)).Color(Colors.Black).Append(Chr(0xF006)).PopAll
    Label5.AttributedText = cs5
    Label6.Text = "2 Stars"
    
End Sub

Sub Label1_Click
    Dim cs As CSBuilder
    cs.Initialize.Font(Font.CreateFontAwesome(22)).Color(Colors.Black).Append(Chr(0xF005)).PopAll
    Label1.AttributedText = cs
    
    Dim cs2 As CSBuilder
    cs2.Initialize.Font(Font.CreateFontAwesome(22)).Color(Colors.Black).Append(Chr(0xF006)).PopAll
    Label2.AttributedText = cs2
    
    Dim cs3 As CSBuilder
    cs3.Initialize.Font(Font.CreateFontAwesome(22)).Color(Colors.Black).Append(Chr(0xF006)).PopAll
    Label3.AttributedText = cs3

    Dim cs4 As CSBuilder
    cs4.Initialize.Font(Font.CreateFontAwesome(22)).Color(Colors.Black).Append(Chr(0xF006)).PopAll
    Label4.AttributedText = cs4
    
    Dim cs5 As CSBuilder
    cs5.Initialize.Font(Font.CreateFontAwesome(22)).Color(Colors.Black).Append(Chr(0xF006)).PopAll
    Label5.AttributedText = cs5
    
    Label6.Text = "1 Star"
End Sub
 
Upvote 0
Top