3 つの画像のオン/オフ/一時停止ボタンをサイクル方式で配置する必要があります。ボタンとラベルを使用してカスタム セルを作成しました。ボタンを押した画像に応じてセルの値を取得する必要があります。
ボタンのサイクルは次のようになります。
オン -> オフ -> 一時停止 -> オン -> オフ
デフォルトの状態は一時停止です。
3 つの画像のオン/オフ/一時停止ボタンをサイクル方式で配置する必要があります。ボタンとラベルを使用してカスタム セルを作成しました。ボタンを押した画像に応じてセルの値を取得する必要があります。
ボタンのサイクルは次のようになります。
オン -> オフ -> 一時停止 -> オン -> オフ
デフォルトの状態は一時停止です。
ボタンの状態の変化に応じてセルのラベルテキストを変更できる列挙型とデリゲートメソッドを使用して、いくつかのプロパティの複数の値を持つ UIButton のサブクラスを作成できます。
typedef enum {
TriButtonValueOn,
TriButtonValueOff,
TriButtonValuePause
} TriButtonValue
@class TriButton;
@protocol TriButtonDelegate <NSObject>
-(void)triButtonStateChanged:(TriButton *)button;
@end
@interface TriButton:UIButton
@property (nonatomic,retain) id<TriButtonDelegate> delegate;
@property (nonatomic) TriButtonValue currentState;
一方、TriButton の .m ファイルでは
-(void)setCurrentState:(TriButtonValue)value{
switch(value){
case TriButtonValueOn:
self.image = ...
break;
case TriButtonValueOff:
self.image = ...
break;
...
...
}
[self.delegate triButtonStateChanged:self];
}
viewController のデリゲート メソッドで
-(void)triButtonStateChanded:(TriButton *)button{
UITableViewCell *cell = (UITableViewCell *)[button superview];
UILabel *textLbl = (UILabel *)[cell viewWithTag:tagOfLabel];
[textLbl setText:@"state change to ..."]; //set it according to button.currentState
}
これは、コントローラーの仕事に聞こえます。
コントローラーは、セル内でボタンを設定する状態を知るための情報を持っているのが理想的です。最初にセル ( 内tableView:cellForRowAtIndexPath:
) を他にどのように作成できますか?
状態を変更する必要がある場合 (ボタンが押されたときはいつでも?)、テーブルビューでコントローラーreloadData
(またはよりきめ細かい行ごとまたはセクションごとのメソッドのいずれか) を呼び出して、UI を更新できます。
これを試して....
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"SearchUserCustomCell";
SearchUserCustomCell *cell = (SearchUserCustomCell *)[tblUser dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
NSArray* nib = [[NSBundle mainBundle] loadNibNamed:@"SearchUserCustomCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.showsReorderControl = NO;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.backgroundColor=[UIColor clearColor];
[cell.button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
}
cell.button.tag = indexpath.row;
}
とボタンクリック方法....
-(IBAction) buttonClick:(id)sender {
UIButton *button = (UIButton *) sender;
if (button.imageView.image == [UIImage imageNamed:@"on.png"])
{
[button setImage:[UIImage imageNamed:@"off.png"] forState:UIControlStateNormal];
}
else if (button.imageView.image == [UIImage imageNamed:@"off.png"])
{
[button setImage:[UIImage imageNamed:@"pause.png"] forState:UIControlStateNormal];
}
…..
}
if ([cell.button.currentImage isEqual:[UIImage imageNamed:@"on.png"] ]){
// set off image
}
else if([cell.button.currentImage isEqual:[UIImage imageNamed:@"off.png"] ]){
// set pause image
}
else{
// set ON image
}