当我尝试设置UIButton实例时发生了崩溃

时间:2013-12-05 02:38:25

标签: ios objective-c uitableview uibutton

这里我有一个自定义的UITableViewcell,其中包含几个UIButton作为子视图添加到其contentview中。之后,当我想在UITableView中分配这些UIButton的标题标签时,却发生了崩溃。(但我可以指定这些按钮的动作来捕捉事件。) 这是我的代码:

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];            
        for (UIView *subviews in cell.contentView.subviews)
        {
            CGRect r;
            UIButton *btttn = (UIButton *)[subviews viewWithTag:subviews.tag];
            switch (subviews.tag) {
                case 101: case 102: case 103: case 104: case 105: case 106: case 107:
                    subviews.layer.masksToBounds = NO ;
                    subviews.backgroundColor = [ UIColor whiteColor ];

                    //[btttn setTitle:@"0" forState:UIControlStateNormal];// Crash happened here!
                    [btttn addTarget:self action:@selector (OnWorkHourClick:) forControlEvents:UIControlEventTouchUpInside]; //This line works if without upper line.
                    r = subviews.frame;
                case 12:
                    subviews.layer.masksToBounds = NO ;
                    subviews.backgroundColor = [ UIColor whiteColor ];
                    [btttn addTarget:self action:@selector (OnJobAddClick:) forControlEvents:UIControlEventTouchUpInside];
                    r = subviews.frame;
            }
        }
return cell;}

0 个答案:

没有答案