strange problem draw a label on UITableViewCell
I used an UINavigationController pushViewController to present an ViewController, on the view of the viewcontroller, there is an UITablView. I used the codes below to draw UILabel on UITableViewCell, but it does not display, only when I tap the row where the UIlabel is, it will appear, if I tap another row, it disappear again.
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *SimpleTableIdentifier1 = @"CellTableIdentifier";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier1 ];
if (cell == nil)
{
cell=[[[UITableViewCell alloc] initWithFrame:CGRectZero
reuseIdentifier:SimpleTableIdentifier1] autorelease];
switch([[myArray objectAtIndex:indexPath.row] getValueType])
{
case _PSToggleSwitchSpecifier:
{
UISwitch *switchview=[[UISwitch alloc]init];
switchview.tag=11106 ;
cell.accessoryView= switchview;
//switchview.frame=CGRectMake(80.9f,15.0f,80.0f,23.0f) ;
//[cell.contentView addSubview:switchview];
[switchview addTarget:self action:@selector(switchValueChanged:) forControlEvents:UIControlEventValueChanged];
[switchview release];
break;
}
case _PSMultiValueSpecifier:
{
cell.accessoryType= UITableViewCellAccessoryDisclosureIndicator;//
//draw a label, but actually it does display,
UILabel *labelCell =[ [UILabel alloc]init];
[labelCell setTag:11101];
labelCell.frame = CGRectMake(80.9f,15.0f,80.0f,23.0f) ;
[labelCell setBackgroundColor:[UIColor clearColor]];
labelCell.textAlignment=UITextAlignmentRight;
[labelCell setFont:[UIFont systemFontOfSize:18.0]];
[labelCell setTextColor:[UIColor blackColor]];
[cell.contentView addSubview:labelCell];
[labelCell release];
break;
}
}
}
cell.text=@"test";
switch([[myArray objectAtIndex:indexPath.row] getValueType])
{
case _PSToggleSwitchSpecifier:
{
cell.accessoryType=UITableViewCellAccessoryNone;//
UISwitch *tem=(UISwitch*) [cell.contentView viewWithTag:11106];//cell.accessoryView;//
tem.on= true;
break;
}
case _PSMultiValueSpecifier:
{
UILabel *newLabelCell=(UILabel*)[cell.contentView viewWithTag: 11101];//intV];
newLabelCell.text=@"Items";//it should display, but actually not, only when I tap the row "didselect" the text display
break;
}
};
return cell;
}
开发者_如何学Go
In codes above UISwitch are displayed, but if I use codes
switchview.frame=CGRectMake(80.9f,15.0f,80.0f,23.0f) ;
[switchview addTarget:self action:@selector(switchValueChanged:) forControlEvents:UIControlEventValueChanged];
[cell.contentView addSubview:switchview];
to replace cell.accessoryView= switchview;
UISwitch will be the same thing as UILabel
Welcome any comment
Thanks
interdev
Try using initWithStyle to create your UITableViewCell...
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
Note: initWithFrame deprecated, and you're using a CGRectZero for the frame
精彩评论