2016-06-27 16 views
0

こんにちは、UITableViewCellに評価星を追加します。評価星をUITableViewCellsに適用する方法

サンプルはありますか?

コードを試しましたが、機能しません。

助けてください。

マイコード:

#import "ViewController.h" 
#import "starsInCell.h" 

@interface ViewController() 
{ 
    starsInCell *cell; 
    NSMutableArray *mainArray,*BtnsArray; 
} 
@end 

@implementation ViewController 
@synthesize TableList; 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    TableList.delegate = self; 
    TableList.dataSource = self; 
    mainArray = [[NSMutableArray alloc] initWithObjects:@"",@"", @"",@"",@"",nil]; 
    BtnsArray = [[NSMutableArray alloc] initWithObjects:@"false",@"false",@"false",@"false",@"false", nil]; 
} 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 

    return mainArray.count; 
} 
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 

    static NSString *cellIdentifier = @"Cell"; 
    cell =[TableList dequeueReusableCellWithIdentifier:cellIdentifier]; 

    if (cell == nil) 
    { 

     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"starsInCell" owner:self options:nil]; 
     cell = [nib objectAtIndex:0]; 
    } 
    //cell.textLabel.text = @"1"; 
    //cell.backgroundColor = [UIColor orangeColor]; 


    NSString *btn1str = [BtnsArray objectAtIndex:0]; 
    NSString *btn2str = [BtnsArray objectAtIndex:1]; 
    NSString *btn3str = [BtnsArray objectAtIndex:2]; 
    NSString *btn4str = [BtnsArray objectAtIndex:3]; 
    NSString *btn5str = [BtnsArray objectAtIndex:4]; 

    [self setImages:btn1str :cell.btn1]; 
    [self setImages:btn2str :cell.btn2]; 
    [self setImages:btn3str :cell.btn3]; 
    [self setImages:btn4str :cell.btn4]; 
    [self setImages:btn5str :cell.btn5]; 


    [cell.btn1 addTarget:self action:@selector(btn1Clicked:) forControlEvents:UIControlEventTouchUpInside]; 
    [cell.btn2 addTarget:self action:@selector(btn2Clicked:) forControlEvents:UIControlEventTouchUpInside]; 
    [cell.btn3 addTarget:self action:@selector(btn3Clicked:) forControlEvents:UIControlEventTouchUpInside]; 
    [cell.btn4 addTarget:self action:@selector(btn4Clicked:) forControlEvents:UIControlEventTouchUpInside]; 
    [cell.btn5 addTarget:self action:@selector(btn5Clicked:) forControlEvents:UIControlEventTouchUpInside]; 

    return cell; 
} 
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ 

    return 80; 
} 

-(void)btn1Clicked:(UIButton*)button{ 
    NSLog(@"Action performed"); 
    //NSLog(@"button position is =====> %d",mainArray.count); 

    CGPoint buttonPosition = [button convertPoint:CGPointZero toView:TableList]; 
    NSIndexPath *indexPath = [TableList indexPathForRowAtPoint:buttonPosition]; 
    int buttonValue = button.tag; 
    NSString *string1 = [BtnsArray objectAtIndex:buttonValue]; 
    for (int i=0; i < mainArray.count; i++) { 
     if (indexPath.row == i){ 

      if ([string1 isEqualToString:@"false"]){ 
       [BtnsArray replaceObjectAtIndex:buttonValue withObject:@"true"]; 
      }else{ 
       for (int i = buttonValue+1; i < BtnsArray.count; i++){ 
        [BtnsArray replaceObjectAtIndex:i withObject:@"false"]; 
       } 
      } 
      break; 
     } 
    } 

    [TableList reloadData]; 

} 
-(void)btn2Clicked:(UIButton*)button{ 
    NSLog(@"Action performed"); 
    //NSLog(@"button position is =====> %d",mainArray.count); 

    CGPoint buttonPosition = [button convertPoint:CGPointZero toView:TableList]; 
    NSIndexPath *indexPath = [TableList indexPathForRowAtPoint:buttonPosition]; 
    int buttonValue = button.tag; 
    NSString *string1 = [BtnsArray objectAtIndex:buttonValue]; 
    for (int i=0; i < mainArray.count; i++) { 
     if (indexPath.row == i){ 

      if ([string1 isEqualToString:@"false"]){ 
       [BtnsArray replaceObjectAtIndex:buttonValue withObject:@"true"]; 
      }else{ 
       for (int i = buttonValue+1; i < BtnsArray.count; i++){ 
        [BtnsArray replaceObjectAtIndex:i withObject:@"false"]; 
       } 
      } 
      break; 
     } 
    } 

    [TableList reloadData]; 

} 
-(void)btn3Clicked:(UIButton*)button{ 
    NSLog(@"Action performed"); 
    //NSLog(@"button position is =====> %d",mainArray.count); 

    CGPoint buttonPosition = [button convertPoint:CGPointZero toView:TableList]; 
    NSIndexPath *indexPath = [TableList indexPathForRowAtPoint:buttonPosition]; 
    int buttonValue = button.tag; 
    NSString *string1 = [BtnsArray objectAtIndex:buttonValue]; 
    for (int i=0; i < mainArray.count; i++) { 
     if (indexPath.row == i){ 

      if ([string1 isEqualToString:@"false"]){ 
       [BtnsArray replaceObjectAtIndex:buttonValue withObject:@"true"]; 
      }else{ 
       for (int i = buttonValue+1; i < BtnsArray.count; i++){ 
        [BtnsArray replaceObjectAtIndex:i withObject:@"false"]; 
       } 
      } 
      break; 
     } 
    } 

    [TableList reloadData]; 

} 
-(void)btn4Clicked:(UIButton*)button{ 
    NSLog(@"Action performed"); 
    //NSLog(@"button position is =====> %d",mainArray.count); 

    CGPoint buttonPosition = [button convertPoint:CGPointZero toView:TableList]; 
    NSIndexPath *indexPath = [TableList indexPathForRowAtPoint:buttonPosition]; 
    int buttonValue = button.tag; 
    NSString *string1 = [BtnsArray objectAtIndex:buttonValue]; 
    for (int i=0; i < mainArray.count; i++) { 
     if (indexPath.row == i){ 

      if ([string1 isEqualToString:@"false"]){ 
       [BtnsArray replaceObjectAtIndex:buttonValue withObject:@"true"]; 
      }else{ 
       for (int i = buttonValue+1; i < BtnsArray.count; i++){ 
        [BtnsArray replaceObjectAtIndex:i withObject:@"false"]; 
       } 
      } 
      break; 
     } 
    } 

    [TableList reloadData]; 

} 
-(void)btn5Clicked:(UIButton*)button{ 
    NSLog(@"Action performed"); 
    //NSLog(@"button position is =====> %d",mainArray.count); 

    CGPoint buttonPosition = [button convertPoint:CGPointZero toView:TableList]; 
    NSIndexPath *indexPath = [TableList indexPathForRowAtPoint:buttonPosition]; 
    int buttonValue = button.tag; 
    NSString *string1 = [BtnsArray objectAtIndex:buttonValue]; 
    for (int i=0; i < mainArray.count; i++) { 
     if (indexPath.row == i){ 

      if ([string1 isEqualToString:@"false"]){ 
       [BtnsArray replaceObjectAtIndex:buttonValue withObject:@"true"]; 
      }else{ 
       for (int i = buttonValue+1; i < BtnsArray.count; i++){ 
        [BtnsArray replaceObjectAtIndex:i withObject:@"false"]; 
       } 
      } 
      break; 
     } 
    } 

    [TableList reloadData]; 

} 
-(void)setImages:(NSString*)string1 :(UIButton*)button1{ 

    if ([string1 isEqualToString:@"false"]){ 
     [button1 setImage:[UIImage imageNamed:@"Star1.png"] forState:UIControlStateNormal]; 
    }else{ 
     [button1 setImage:[UIImage imageNamed:@"Star2.png"] forState:UIControlStateNormal]; 
    } 
} 

@end 
+1

これを実行しようとしたコードを表示します。選択した状態とデフォルトの状態の両方に2つのイメージを設定したボタンを使用し、それに応じてイメージを管理することができます。 –

+0

あなたは、レートを与えたり、単に評価を表示したりすることを許可しますか? – ivarun

+0

こんにちはivarun、はい。私はユーザーが評価を与えることを許可したい。 –

答えて

0

は、私はあなたが開発中の新機能で、あなたのロジックを構築したい場合にのみ、この自分自身を行うには、あなたをお勧めしますので、代わりに任意の既製のライブラリを使用してのあなたが手順の下に従うことができます。

  1. まず、各ボタンのデフォルト状態のためのあなたの細胞とセット境スター画像に5つのボタンを追加し、選択設定が満たされた星像にボタンの状態を変更します。 (5つのボタンに対して同じ操作を行います)。これを行った後、すべてのボタンの状態をデフォルトに変更してください。

  2. アクションを追加buttonStarRatingsClickedボタン5つのボタンすべてに対してをクリックします。

  3. starsInCellクラスのボタンごとにコンセントコレクションbuttonCollectionStarRatingsを作成します。

  4. 各セルに星があるtableViewを表示しているviewControllerに以下のメソッドを追加します。

    - (IBAction)buttonStarRatingsClicked:(id)sender { 
    
    UIButton *buttonStarRatings = (UIButton*) sender; 
    
    //Here you will need to get cell to access buttonCollectionStarRatings 
    UITableViewCell *cell = (UITableViewCell*) [[buttonStarRatings superView] superView]; 
    //Check is clicked button is selected 
    if ([buttonStarRatings isSelected]) { 
    
    
    if (buttonStarRatings.tag == 0) { //Check is clicked button is first button 
    
        //Check if Next button of selected button is selected 
        if ([cell.buttonCollectionStarRatings[buttonStarRatings.tag+1] isSelected]) { 
    
         @autoreleasepool { 
    
          for (int i = (int)buttonStarRatings.tag+1; i < [cell.buttonCollectionStarRatings count]; i++) { 
           UIButton *buttonStarRating = cell.buttonCollectionStarRatings[i]; 
           [buttonStarRating setSelected:NO]; 
          } 
         } 
    
        } else { 
         //If selected button is first button and is selected then do not take any action as atlease one star should be selected. 
        } 
    
    } else if (buttonStarRatings.tag == 4) { //Check is clicked button is last button 
    
        [buttonStarRatings setSelected:NO]; 
    
    } else { //Selected button is one of the button between first and last 
    
        //Check if Next button of selected button is selected 
        if ([cell.buttonCollectionStarRatings[buttonStarRatings.tag+1] isSelected]) { 
    
         @autoreleasepool { 
    
          for (int i = (int)buttonStarRatings.tag; i < [cell.buttonCollectionStarRatings count]; i++) { 
           UIButton *buttonStarRating = cell.buttonCollectionStarRatings[i]; 
           [buttonStarRating setSelected:NO]; 
          } 
         } 
        } else { 
         [buttonStarRatings setSelected:NO]; 
        } 
    } 
    } else { 
    
    if (buttonStarRatings.tag == 0) { //Check is clicked button is first button 
    
        [buttonStarRatings setSelected:YES]; 
    } else if (buttonStarRatings.tag == 4) { //Check is clicked button is last button 
    
        @autoreleasepool { 
    
         for (UIButton *buttonStarRating in cell.buttonCollectionStarRatings) { 
          [buttonStarRating setSelected:YES]; 
         } 
        } 
    } else { //Selected button is one of the button between first and last 
    
        @autoreleasepool { 
    
         for (int i = 0; i <= (int)buttonStarRatings.tag; i++) { 
          UIButton *buttonStarRating = cell.buttonCollectionStarRatings[i]; 
          [buttonStarRating setSelected:YES]; 
         } 
        } 
    } 
    } 
    } 
    

上の部分は、テーブルをスクロールしながら次のステップは、各セルに選択した星を管理している、あなたは、各セルの星評価を選択することができるようになります。

関連する問題