如何在表格视图中设置三个图像开/关/暂停按钮并在iphone中相应获取值

时间:2013-03-28 06:33:31

标签: iphone objective-c uibutton tableview

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

按钮循环如下:

  

on - > off - >暂停 - > on - > off

默认状态为暂停。

4 个答案:

答案 0 :(得分: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
    }

答案 1 :(得分:1)

这听起来像你的控制器。

理想情况下,控制器可以获取信息以了解在单元格中设置按钮的状态 - 您还可以在第一时间创建单元格(在tableView:cellForRowAtIndexPath:中)?

当状态需要更改时(每当按下按钮?),您可以让控制器在其tableview上调用reloadData(或者每行或每个部分的细粒度方法之一)更新用户界面。

答案 2 :(得分: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
}

答案 3 :(得分: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];
            }
            …..
    }