Как изменить цвет ячейки UITableViewCell на синий, когда она выбрана

#objective-c #iphone #uitableview

#objective-c #iPhone #uitableview

Вопрос:

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

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

  - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {

    static NSString *MyIdentifier = @"dealsCC";
    dealsCC *cell = (dealsCC *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    [cell selectionStyle:UITableViewCellSelectionStyleBlue];
}
  

Пожалуйста, скажите мне,

заранее спасибо.

Ответ №1:

Установите selectionStyle.

 [cell setSelectionStyle:UITableViewCellSelectionStyleBlue];
  

Ответ №2:

в didSelectRowAtIndexPath вы должны написать этот код

 UITableViewCell *cell = [table cellforRowAtIndexPath:indexPath.row];
[cell setSelectionStyle:UITableViewCellSelectionStyleBlue];
  

Комментарии:

1. Я пробовал это, но это не работает, есть ли какой-либо другой метод?

2. напишите приведенный ниже код в cellForRowAtIndexPath [cell setSelectionStyle:UITableViewCellSelectionStyleBlue];

3. статическая строка NSString *MyIdentifier = @»dealsCC»; dealsCC *ячейка = (dealsCC *)[TableView dequeueReusableCellWithIdentifier:MyIdentifier]; [TableView отменяет выделение в TableView: indexPath.строка анимирована: ДА]; [стиль выбора ячейки: UITableViewCellSelectionStyleBlue];

4. удалите строку [tableview deselectrowAtindexpath:indexPath.строка анимирована: ДА];// это делается для удаления выделения ячейки tableviewcell

5. очистите целевые объекты и запустите его.

Ответ №3:

 // Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"myCellId";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        UIView *v = [[[UIView alloc] init] autorelease];
        v.backgroundColor = [UIColor redColor];
        **cell.selectedBackgroundView = v;**
    }
    // Set up the cell...
    cell.textLabel.text = @"foo";
    return cell;
}
  

Ответ №4:

Да. Я делаю это следующим образом

     UIView *bgColorSelected = [[UIView alloc] init];
    [bgColorSelected setBackgroundColor:[UIColor colorWithRed:180.0f/255.0f green:36.0f/255.0f blue:21.0f/255.0f alpha:1.0f]];
    cell.selectedBackgroundView = bgColorSelected;
    [bgColorSelected release];
    cell.backgroundColor = [ UIColor clearColor]; 
  

Ответ №5:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell =
    [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:CellIdentifier];
    }

    cell.selectionStyle = UITableViewCellSelectionStyleBlue;

    return cell;
}
  

Ответ №6:

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

{
    cell.selectionStyle = UITableViewCellSelectionStyleBlue;
}