0

我需要以循环方式将三个图像打开/关闭/暂停按钮。我创建了一个带有按钮和标签的自定义单元格。我需要根据按钮上按下的图像获取单元格的值。

按钮循环就像:

开->关-->暂停-->开-->关

默认状态是暂停。

4

4 回答 4

1
  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
    }
于 2013-03-28T06:40:37.477 回答
1

这听起来适合您的控制器。

理想情况下,控制器将拥有知道在单元格中将按钮设置为什么状态的信息——否则您首先如何创建单元格(in tableView:cellForRowAtIndexPath:)?

当状态需要更改时(无论何时按下按钮?),您可以reloadData在其 tableview 上调用控制器(或更细粒度的每行或每节方法之一)来更新 UI。

于 2013-03-28T06:50:32.747 回答
1

您可以使用枚举类型和委托方法创建具有某些属性的多个值的 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
}
于 2013-03-28T07:00:16.560 回答
1

试试这个....

    -(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];
            }
            …..
    }
于 2013-03-28T07:06:57.300 回答