Hi,
I want to add UILabel and UIButton in UITableViewCell. I add following code to do so.
Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
NSInteger row=[indexPath row];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 15, 150, 25)];
[label setBackgroundColor:[UIColor grayColor]];
label.text=[m_pData objectAtIndex:row];
label.font = [UIFont boldSystemFontOfSize:18];
[cell.contentView addSubview:label];
[label release];
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(160, 10, 150, 30)];
[btn setBackgroundColor:[UIColor redColor]];
[btn actionsForTarget:@selector(btnClicked:) forControlEvent:UIControlEventTouchUpInside];
[btn setTitle: @"Test" forState:UIControlStateNormal];
forControlEvent:UIControlEventTouchUpInside];
[cell.contentView addSubview:btn];
[btn release];
return cell;
}
But the button appears as label. I have set the selector and even that doesn't get called.
Also there is another issue. When I scroll through the table item (scroll up and down ) multiple times, I see multiple overlapping text for same cell. It is due to reusing cell item?
Thanks