0

我在表格中创建了 1)customcell,单元格内有 1 个按钮和 1 个滑块。

           NSString *CellIdentifier = [NSString stringWithFormat:@"%d",indexPath.row];

        if (obj_TextCell == nil) {                
                        obj_TextCell = [[[TextCell alloc] initWithStyle:UITableViewCellStyleDefault
                                                       reuseIdentifier:CellIdentifier] autorelease];

           }
    UIButton* Btn_music=[[UIButton alloc] initWithFrame:CGRectMake(130,180, 50,50)];

                if (indexPath.row==play_tg)
                {
                     [Btn_music setImage:[UIImage imageNamed:@"pausemusic.png"] forState:UIControlStateNormal];

                }else
                {
                    [Btn_music setImage:[UIImage imageNamed:@"playmusic.png"] forState:UIControlStateNormal];
                }             

              [Btn_music addTarget:self action:@selector(playAudio:) forControlEvents:UIControlEventTouchUpInside];
              Btn_music.tag = indexPath.row;
              [obj_TextMusicCell.contentView addSubview:Btn_music];
              [background bringSubviewToFront:Btn_music];
 UISlider *progress = [[UISlider alloc] initWithFrame:CGRectMake(180,195,320,10)];
            [progress setBackgroundColor:[UIColor clearColor]];
            progress.minimumValue = 0.0;
            progress.maximumValue=1.0;
            progress.continuous = YES;
            progress.value = 0.0;
            progress.tag=indexPath.row+1;
            NSLog(@"tag for slider========>%d",progress.tag);
            //[progress setTag:indexPath.row];
            [progress addTarget:self action:@selector(sliderChanged:) forControlEvents:UIControlEventValueChanged];
            [obj_TextMusicCell.contentView addSubview:progress];

2)我已经获取单元格的按钮的 IBACTION 事件。并调用了滑块​​移动的方法。

-(IBAction)playAudio:(id)sender
{
    UIButton *Btn_music=sender;    

    cell_Access = (TextMusicCell *)[tbl_tellTale viewWithTag:0];
    NSIndexPath *indexPath = [tbl_tellTale indexPathForCell:cell_Access];
     NSLog(@"cellindex==>%d",indexPath.row); 
if ( timer==nil)
        {
          timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(SongProgress) userInfo:nil repeats:YES];
           [timer fire];
        }

}

3)滑块中的方法有:-

-(IBAction)SongProgress
{

   UISlider* progress = (UISlider *)[cell_Access viewWithTag:1];
}

4)它工作得很好。但是当我滚动表格视图或有时应用程序崩溃并给出以下错误: -

-[UIButton setValue:]: unrecognized selector sent to instance 0x96f9660
2013-02-20 19:54:21.008 shc[3776:19d03] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIButton setValue:]: unrecognized selector sent to instance 0x96f9660'
*** First throw call stack:
(0x3dba012 0x2777e7e 0x3e454bd 0x3da9bbc 0x3da994e 0x108d1b 0x21c0b90 0x3d79376 0x3d78e06 0x3d60a82 0x3d5ff44 0x3d5fe1b 0x33de7e3 0x33de668 0x16bf65c 0x2cb6 0x2be5 0x1)
libc++abi.dylib: terminate called throwing an exception

-[UIButton setValue:]: 无法识别的选择器发送到实例

请帮我

4

2 回答 2

0

您为 UIBUTTON* btn_music 和 UISlider *progress 使用相同的标签值,因此 UISlider *progress 获取 UIBUTTON 实例对两者使用不同的标签值,例如如果您为 UIBUTTON 使用 1 到 200 个标签值,然后为 UISlider *progress 使用 201 加值

于 2013-02-22T09:05:09.313 回答
-1

您可能正在做第一部分- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath,如果是这样,您正在创建新的UIButtons并且UISliders每次UITableView需要渲染该单元格时......

UIButton您应该为and添加属性UISlider并检查它们是否已初始化...

编辑:

好的,如果你有属性Btn_musicprogress在你的自定义 UITableViewCell 中定义,你应该合成它们,然后像​​这样初始化它们:

// If the Button is not yet initialized, then do it
if(obj_TextMusicCell.Btn_music == nil)
{
    // Here you should do the initialization code that only have to be done the first time
    obj_TextMusicCell.Btn_music=[[UIButton alloc] initWithFrame:CGRectMake(130,180, 50,50)];
    obj_TextMusicCell.Btn_music.tag = indexPath.row;
    [obj_TextMusicCell.contentView addSubview:Btn_music];
}

// Now it already exists, just update it...
if(indexPath.row==play_tg)
{
    [obj_TextMusicCell.Btn_music setImage:[UIImage imageNamed:@"pausemusic.png"] forState:UIControlStateNormal];
}
else
{
    [obj_TextMusicCell.Btn_music setImage:[UIImage imageNamed:@"playmusic.png"] forState:UIControlStateNormal];
}

遵循相同的想法,UISlider你就明白了......

于 2013-02-20T14:49:25.780 回答