这里我有一个自定义的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;}