Reputation: 631
I want to create UITableView where some cells are buttons. Help me, what is the right way to do it?
1) I can use something like that:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
}
if (indexPath.section == 1){
if (indexPath.row == 0){
UIButton* loginButton = [[UIButton alloc] initWithFrame:CGRectMake(9,1,302, 48)];
[loginButton setBackgroundImage:[UIImage imageNamed:@"LoginButton.png"] forState:UIControlStateNormal];
[loginButton setBackgroundImage:[UIImage imageNamed:@"LoginButton_pressed.png"] forState:UIControlStateSelected];
[loginButton setTitle:@"Login" forState:UIControlStateNormal];
[cell setBackgroundColor:[UIColor clearColor]];
[cell addSubview:loginButton];
[loginButton release];
}
}
but I see that button is transparent and looks strange.
2) don't use any UIButtons and make what I want entirely using UITableViewCells
Thank you four your attention, I'm completely new for iOS, but I'm trying to make app without xib.
Upvotes: 0
Views: 202
Reputation: 1918
you have not set button type ,it is read only property .so you can not direct assign it so change your code like this:
UIButton* loginButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[loginButton setFrame:CGRectMake(5,5,302, 34)];
[loginButton setBackgroundImage:[UIImage imageNamed:@"LoginButton.png"] forState:UIControlStateNormal];
[loginButton setBackgroundImage:[UIImage imageNamed:@"LoginButton_pressed.png"] forState:UIControlStateSelected];
[loginButton setTitle:@"Login" forState:UIControlStateNormal];
[cell setBackgroundColor:[UIColor clearColor]];
[cell addSubview:loginButton];
Upvotes: 1