iPhone -按钮消失了



我有一个表视图。在一个单元格上,我添加一个按钮(UIButton customButton)。但有时这个按钮会消失。我检查了整个代码,我没有修改按钮属性。我还保留了按钮。实际上按钮仍然在那里,但不可见。如果我点击按钮的位置按钮上的回调函数被调用。我不知道哪里出了问题。我从2天检查这个,但没有发现任何错误。我把它粘贴在代码下面。如果有人遇到过这个问题,请帮助我!!

    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];

添加完整代码。

 - (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;
 }

//自定义表视图单元格的外观

 - (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;
  }

正如你所说,它在那里,但你看不到它,与它相关联的方法被调用(SignOut)。输入这一行

[logOutBtn setBackgroundColor:[UIColor redColor]];

如果你的按钮显示红色矩形形状,那么有一个问题在图像中,要么你没有给出正确的名称或任何问题在该图像。检查BLUE.png的拼写。

也是区分大小写的,所以也检查一下。拼字没关系,可以用其他图片。

[UIImage imageNamed:]又名+ (UIImage)imageNamed:(NSString *)name是一个方便的方法,返回一个自动释放的项目,这意味着图像可以在它进入按钮之前被释放。

尝试使用[[UIImage imageNamed:@"BLUE.png"] retain][[UIImage alloc] initWithContentsOfFile:@"BLUE.png"]

相关内容

最新更新