UITableView附件上的图标可以替换吗?

时间:2010-11-23 03:48:50

标签: iphone cocoa-touch ios uikit

我有一个表视图,在一种情况下我想使用UITableViewCellAccessoryDe​​tailDisclosureButton使用附件,但我想用我的图标更改该图标,我能做到吗?

1 个答案:

答案 0 :(得分:8)

是的....您可以通过添加自定义按钮轻松完成...

  1. 创建自定义按钮,
  2. 设置按钮图像
  3. 将此按钮添加到accessoryView
  4. 设置按钮的方法
  5. 我正在为您提供一些代码帮助:

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    
        static NSString *CellIdentifier = @"Cell";
       cell =(Custom *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
        if (cell == nil) {
            cell = [[[Custom alloc]  initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
            [cell setBackgroundColor:[UIColor clearColor]];
            menuTable.separatorColor =[UIColor clearColor];
    
        }
        UIImage *image = [UIImage imageNamed:@"ButtonClickOrder.png"];
    
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        CGRect frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
        button.frame = frame;   // match the button's size with the image size
    
        //[button setBackgroundImage:image forState:UIControlStateNormal];
        [button setImage:image forState:UIControlStateNormal];
        // set the button's target to this table view controller so we can interpret touch events and map that to a NSIndexSet
        [button addTarget:self action:@selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside];
        button.backgroundColor = [UIColor clearColor];
        cell.accessoryView = button;
    
           return cell;
    }
    
    //These are the methods which are called with the AccessoryView is Clicked
    
    - (void)checkButtonTapped:(id)sender event:(id)event
    {
    
        NSSet *touches = [event allTouches];
        UITouch *touch = [touches anyObject];
        CGPoint currentTouchPosition = [touch locationInView:menuTable];
        NSIndexPath *indexPath = [menuTable indexPathForRowAtPoint: currentTouchPosition];
        NSLog(@"Section:%d",indexPath.section);
        NSLog(@"Index:%d",indexPath.row);
        if (indexPath != nil)
        {
             [ self tableView: menuTable accessoryButtonTappedForRowWithIndexPath: indexPath];
    
        }
    
    }
    
    - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
    {   
    
            NSLog(@"%d",indexPath.section);
    
    }
    

    希望这肯定会对你有用....