iPhone - Button getting dissappeard
I am having开发者_开发知识库 a table view . On a cell I am adding a button ( UIButton customButton ). But sometimes that button disappears. I checked the whole code , nowhere I am modifying the button properties. I have also retained the button . Actually button is still there but not visible. If I click on buttons location the call back function on button gets called. I don't know what's going wrong. I have checking this from 2 days but didn't find any bug. I am pasting below the code for that. If anyone has faced this issue , please help me !!!
logOutBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[logOutBtn setImage:[UIImage imageNamed:@"BLUE.png"] forState:UIControlStateNormal];
[logOutBtn setFrame:CGRectMake(100, 2, 100, 35)];
[logOutBtn addTarget:self action:@selector(SignOut) forControlEvents:UIControlEventTouchUpInside];
[logOutBtn retain];
Adding the complete code.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
switch (section) {
case 0:
return 1;
break;
case 1:
return 4;
break;
case 2:
return 1;
break;
}
return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0) {
return 90;
}
return 50;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = [NSString stringWithFormat:@"%d%d",indexPath.section,indexPath.row];
int newCellFlag = 0;
UITableViewCell *cell;
UILabel * titleLabel;
UIImageView * logoView;
UIButton * infoBtn;
// Configure the cell...
switch (indexPath.section) {
case 0:
//add the custom button for cities
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
newCellFlag = 1;
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
if (newCellFlag == 1)
{
if (indexPath.row==0) {
/*cell.textLabel.textAlignment = UITextAlignmentCenter;
cell.textLabel.text = @"My city subscriptions";*/
cell.selectionStyle = UITableViewCellSelectionStyleNone;
logoView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"logo.png"]];
logoView.frame = CGRectMake(5, 0, 295, 80);
[cell.contentView addSubview:logoView];
[logoView release];
infoBtn = [UIButton buttonWithType:UIButtonTypeInfoDark];
infoBtn.frame = CGRectMake(270, 75, 15, 15);
[cell.contentView addSubview:infoBtn];
[infoBtn addTarget:self action:@selector(infoView:) forControlEvents:UIControlEventTouchUpInside];
}
if (indexPath.row ==1) {
cell.detailTextLabel.textAlignment = UITextAlignmentCenter;
cell.detailTextLabel.textColor = [UIColor orangeColor];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.detailTextLabel.text = @"Add cities";
//[cell.contentView addSubview:_AddCitiesButton];
}
}
break;
case 1:
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
newCellFlag = 1 ;
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
//add the custom button for cities
if (newCellFlag == 1)
{
if (indexPath.row ==0) {
cell.textLabel.text = @"User name";
cell.detailTextLabel.text =[[NSUserDefaults standardUserDefaults] objectForKey:@"user_name"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
if (indexPath.row ==1) {
cell.textLabel.text = @"Credits left";
cell.detailTextLabel.text = (connected ? [NSString stringWithFormat:@"$ %@",[self getUserMoney]] : @"Not Available.");
cell.selectionStyle = UITableViewCellSelectionStyleNone;
//[cell.contentView addSubview:_creditsLabel];
}
if (indexPath.row ==2) {
cell.textLabel.text = @"Current City";
cell.detailTextLabel.text = [[NSUserDefaults standardUserDefaults]objectForKey:@"current_city_name"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
//[cell.contentView addSubview:_currentCity];
}
if (indexPath.row == 3) {
/*
titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 5, 100, 35)];
titleLabel.text = @"Log out";
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.textColor = [UIColor purpleColor];
titleLabel.font = [UIFont fontWithName:@"Verdana" size:15.5];
titleLabel.font = [UIFont boldSystemFontOfSize:15.5];
[cell.contentView addSubview:titleLabel];
[titleLabel release];
*/
[cell.contentView addSubview:logOutBtn];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
}
break;
case 2:
//add the custom button for cities
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
newCellFlag = 1;
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
if (newCellFlag ==1 ) {
cell.textLabel.text = @"App Version";
cell.detailTextLabel.text = @"1.0";
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
break;
default:
break;
}
if(newCellFlag == 1)
{
if (indexPath.section == 0) {
CommonBG * bg = [[CommonBG alloc]initWithFrame:cell.bounds];
cell.backgroundView = bg;
cell.backgroundView.alpha = 0.0;//[UIColor clearColor];
[bg release];
}
else {
CommonBG * bg = [[CommonBG alloc]initWithFrame:cell.bounds];
cell.backgroundView = bg;
[bg release];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
cell.layer.cornerRadius = 10;
}
}
return cell;
}
As you said it is there but you can't see it, and method associated with it being called(SignOut). so put this line
[logOutBtn setBackgroundColor:[UIColor redColor]];
if your button shows with red rectangular shape then there is a problem in image either you are not giving name correct or any problem in that image. check spelling of BLUE.png .
also it is case sensitive so check this also. and spelling is not matter then try to use other image.
[UIImage imageNamed:]
aka + (UIImage)imageNamed:(NSString *)name
is a convenience method returns an autoreleased item, which means that the image can be released before it makes it to the button.
Try using [[UIImage imageNamed:@"BLUE.png"] retain]
or [[UIImage alloc] initWithContentsOfFile:@"BLUE.png"]
.
精彩评论