0

Facebookアプリに似たサイドバーにテーブルビューがあります。私のサイドバーのテーブルビューでは、各行にイメージビューがあります。セルをクリックするとイメージビューが変更され、別のセルを選択している間は保持されます。これが私のコードです

 - (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]autorelease];
}
[[cell.contentView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];

UIView * contentView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 60)];

UIImageView * cellImg = [[UIImageView alloc]initWithFrame:CGRectMake(5,8,259, 60)];
[cellImg setImage:[UIImage imageNamed:[menuArr objectAtIndex:indexPath.row]]];
[contentView addSubview:cellImg];

[contentView setBackgroundColor:[UIColor clearColor]];
[cell.contentView addSubview:contentView];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}

 #pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
delegate.sideBarSelectedIndex = indexPath.row;
NSLog(@"delegate.sideBarSelectedIndex: %d",delegate.sideBarSelectedIndex);

[UIView commitAnimations];
if (self.sidebarDelegate) {
    NSObject *object = [NSString stringWithFormat:@"ViewController%d", indexPath.row];
    [self.sidebarDelegate sidebarViewController:self didSelectObject:object atIndexPath:indexPath];
    [delegate.firstLensePageObj timerFunction:indexPath.row];
}

}
4

3 に答える 3

1

デリゲートメソッドでこれを試してくださいdidSelectRowAtIndexPath:-

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.imageView.image = [UIImage imageNamed:@"yourImage"];
于 2013-10-31T05:45:00.567 に答える
0

cellForRowAtIndexPath でこれを試してください。

  UIImageView *selectedImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Your Image"]];

  cell.selectedBackgroundView = selectedImageView;
于 2013-10-31T05:56:12.383 に答える
0

didSelectRowAtIndexPath:メソッドで選択したインデックスパスをキャプチャする必要があり、そのメソッドでテーブルビューをリロードする必要があります。cellForRowAtIndexPathでは、選択したインデックスを現在のインデックスでチェックする必要があります。

    - (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]autorelease];
    }
    [[cell.contentView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];

    UIView * contentView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 60)];

    UIImageView * cellImg = [[UIImageView alloc]initWithFrame:CGRectMake(5,8,259, 60)];
if (delegate.sideBarSelectedIndex == indexpath.row){
//Selected cell
    [cellImg setImage:[UIImage imageNamed:[selectedMenuArr objectAtIndex:indexPath.row]]];
}else {
//Unselected cell
    [cellImg setImage:[UIImage imageNamed:[menuArr objectAtIndex:indexPath.row]]];
}
    [contentView addSubview:cellImg];

    [contentView setBackgroundColor:[UIColor clearColor]];
    [cell.contentView addSubview:contentView];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
    }

     #pragma mark - Table view delegate

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    delegate.sideBarSelectedIndex = indexPath.row;
    NSLog(@"delegate.sideBarSelectedIndex: %d",delegate.sideBarSelectedIndex);

    [UIView commitAnimations];
    if (self.sidebarDelegate) {
        NSObject *object = [NSString stringWithFormat:@"ViewController%d", indexPath.row];
        [self.sidebarDelegate sidebarViewController:self didSelectObject:object atIndexPath:indexPath];
        [delegate.firstLensePageObj timerFunction:indexPath.row];
    }

    }
于 2016-12-10T06:52:32.500 に答える