こんにちは、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
これを実行しようとしたコードを表示します。選択した状態とデフォルトの状態の両方に2つのイメージを設定したボタンを使用し、それに応じてイメージを管理することができます。 –
あなたは、レートを与えたり、単に評価を表示したりすることを許可しますか? – ivarun
こんにちはivarun、はい。私はユーザーが評価を与えることを許可したい。 –