私は問題があります。テーブルにカスタム セルを表示したい 各セルにはボタンが含まれています (添付ファイルを参照)
メソッド cellForRowAtIndexPath で、カスタム セルを返します。カスタム セルには、添付の画像に示すように UIButton が含まれています。
私の問題は次のとおりです-ボタンを押すと、ボタンが点灯し(赤色に)、他のボタンが減衰する必要があります(青色に)。オブジェクト間の接続を作成する方法は?
どんな答えでも嬉しいです!ありがとう!
私は問題があります。テーブルにカスタム セルを表示したい 各セルにはボタンが含まれています (添付ファイルを参照)
メソッド cellForRowAtIndexPath で、カスタム セルを返します。カスタム セルには、添付の画像に示すように UIButton が含まれています。
私の問題は次のとおりです-ボタンを押すと、ボタンが点灯し(赤色に)、他のボタンが減衰する必要があります(青色に)。オブジェクト間の接続を作成する方法は?
どんな答えでも嬉しいです!ありがとう!
この問題を次のように解決しました。
これは、UITableViewController クラスの私の実装です。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CustomCell";
CustomCell *cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:nil] autorelease];
if (cell == nil) {
cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.delegate = self;
[cells addObject:cell];
// Configure the cell...
return cell;
}
- (void)mustDeselectWithout:(CustomCell *)cell{
for (CustomCell * currentCell in cells) {
if(currentCell != cell){
[currentCell deselect];
}
}
}
セルを作成するとわかるように、メソッド cellForRowAtIndexPath でカスタム セルを返します。
カスタムセルのクラスです
.h
#import <UIKit/UIKit.h>
#import "ProtocolCell.h"
@interface CustomCell : UITableViewCell {
UIButton * button;
}
@property(nonatomic, assign) id<ProtocolCell> delegate;
- (void)select;
- (void)deselect;
@end
そしてM
#import "CustomCell.h"
@implementation CustomCell
@synthesize delegate;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(5, 5, 25, 25)];
[button setBackgroundColor:[UIColor blueColor]];
[button addTarget:self action:@selector(select) forControlEvents:UIControlEventTouchUpInside];
[self.contentView addSubview:button];
}
return self;
}
- (void)select{
[button setBackgroundColor:[UIColor redColor]];
[delegate mustDeselectWithout:self];
}
- (void)deselect{
[button setBackgroundColor:[UIColor blueColor]];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void)dealloc
{
[super dealloc];
}
@end
私のクラス UITableViewController はプロトコルProtocolCellを実装しています。ボタンをタップすると、デリゲートがメソッドmustDeselectWithoutを呼び出し、現在のボタンなしで配列内のすべてのボタンを選択解除します。
#import <Foundation/Foundation.h>
@class CustomCell;
@protocol ProtocolCell <NSObject>
- (void)mustDeselectWithout:(CustomCell *)cell;
@end
これが私の実装です。コメントいただければ、幸いです。これが私の問題の正しい解決策であるかどうかがわからないためです。ありがとう!