iPhone - кнопка исчезает - PullRequest
0 голосов
/ 08 июля 2011

У меня есть вид таблицы.В ячейку я добавляю кнопку (пользовательская кнопка UIButton).Но иногда эта кнопка исчезает.Я проверил весь код, нигде не изменяю свойства кнопки.Я также сохранил кнопку.На самом деле кнопка все еще там, но не видна.Если я нажимаю на расположение кнопок, вызывается функция обратного вызова для кнопки.Я не знаю, что происходит не так.Я проверил это за 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;
  }

Ответы [ 2 ]

0 голосов
/ 08 июля 2011

[UIImage imageNamed:] aka + (UIImage)imageNamed:(NSString *)name - это удобный метод, который возвращает автоматически выпущенный элемент, что означает, что изображение можно разблокировать до того, как оно попадет на кнопку.

Попробуйте использовать [[UIImage imageNamed:@"BLUE.png"] retain] или [[UIImage alloc] initWithContentsOfFile:@"BLUE.png"].

0 голосов
/ 08 июля 2011

Как вы сказали, он есть, но вы его не видите, и вызывается связанный с ним метод (SignOut). так что поставь эту строку

[logOutBtn setBackgroundColor: [UIColor redColor]];

Если ваша кнопка отображается с красной прямоугольной формой, значит, проблема в изображении: либо вы не правильно даете имя, либо в этом изображении. проверить орфографию BLUE.png.

также он чувствителен к регистру, так что проверьте это также. и написание не имеет значения, тогда попробуйте использовать другое изображение.

...