Как установить стиль выделения синим, если в каждую ячейку добавлено фоновое изображение ячейки в iPhone SDK? - PullRequest
1 голос
/ 13 сентября 2010

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = (UITableViewCell *)[myTableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"MyIdentifier"] autorelease];
        UIView* elementView =  [ [UIView alloc] initWithFrame:CGRectMake(5,5,300,480)];
        elementView.tag = 0;
        [cell.contentView addSubview:elementView];
        [elementView release];
    }

    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    cell.selectionStyle=UITableViewCellSelectionStyleNone;

    UIView* elementView  = [cell.contentView viewWithTag:0];
    for(UIView* subView in elementView.subviews)
    {
        [subView removeFromSuperview];
    }

    UIImageView* cellBGImageView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 65)];
    cellBGImageView.image=[UIImage imageNamed:@"cellbg.png" ];
    [elementView addSubview:cellBGImageView];
    [cellBGImageView release];

    Customer *objectForCustomer=[customerList objectAtIndex:indexPath.row];

    UILabel *nameLable =[[UILabel alloc] initWithFrame:CGRectMake(10, 7, 280, 20)];
    [nameLable setFont:[UIFont boldSystemFontOfSize:17]];
    nameLable.textAlignment=UITextAlignmentLeft;
    //nameLable.textColor = [UIColor blackColor];
    nameLable.numberOfLines = 0;
    nameLable.tag=2;
    nameLable.backgroundColor = [UIColor clearColor];
    nameLable.text=objectForCustomer.customerName;
    [elementView addSubview:nameLable];
    [nameLable release];

return cell;
}

Ответы [ 3 ]

0 голосов
/ 13 сентября 2010

Я знаю, что когда у вас есть пользовательская ячейка, вы можете указать ее в IB. Не знаю, возможно ли это из кода (думаю, так и должно быть).

Попробуйте использовать это в своем коде:

[cell setBackgroundView:cellBGImageView.image];

Я скептически отношусь к тому, работает ли оно, но стоит попробовать.

0 голосов
/ 13 сентября 2010

Эй, в вашем коде есть проблема, что вы устанавливаете selectionStyle ячейки как none.

Проблема не с изображением, а проблема с кодом.

Я переписываю код для вашего удобства.Пожалуйста, проверьте это

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

{ UITableViewCell *cell = (UITableViewCell )[myTableView dequeueReusableCellWithIdentifier:@"MyIdentifier"]; 
    if (cell == nil) 
    {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"MyIdentifier"] autorelease]; 
        UIView elementView = [ [UIView alloc] initWithFrame:CGRectMake(5,5,300,480)]; 
        elementView.tag = 0;
        [cell.contentView addSubview:elementView];
        [elementView release]; 
    }

    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
//THIS IS THE LINE I HAVE COMMENTED THAT WAS CREATING THE PROBLEM
//  cell.selectionStyle=UITableViewCellSelectionStyleNone;

    UIView* elementView = [cell.contentView viewWithTag:0];
    for(UIView* subView in elementView.subviews)
    { 
        [subView removeFromSuperview]; 
    }

    UIImageView* cellBGImageView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 65)]; 
    cellBGImageView.image=[UIImage imageNamed:@"cellbg.png" ]; 
    [elementView addSubview:cellBGImageView];
    [cellBGImageView release];

    Customer *objectForCustomer=[customerList objectAtIndex:indexPath.row];

    UILabel *nameLable =[[UILabel alloc] initWithFrame:CGRectMake(10, 7, 280, 20)];
    [nameLable setFont:[UIFont boldSystemFontOfSize:17]]; 

     nameLable.textAlignment=UITextAlignmentLeft; //nameLable.textColor = [UIColor blackColor]; nameLable.numberOfLines = 0;
      nameLable.tag=2; 
      nameLable.backgroundColor = [UIColor clearColor]; 
      nameLable.text=objectForCustomer.customerName; 
     [elementView addSubview:nameLable]; 
     [nameLable release];

    return cell; 
}

СЧАСТЛИВОЕ КОДИРОВАНИЕ ...

0 голосов
/ 13 сентября 2010

Когда вы покрываете всю ячейку изображением, вы не можете видеть выбранную ячейку.Вы должны реализовать пользовательскую ячейку, чтобы обработать (или показать), что какая-то ячейка выбрана.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...