单击时UITableViewCell背景颜色变为白色

时间:2014-07-28 20:08:57

标签: ios uitableview

我有一个UITableViewCell,其中填充了从PHP页面中提取的数组。基于屏幕方向,单元格可以足够宽以适合两个图像而不是仅一个。所以我检测到屏幕方向,然后添加两个图像视图(一个占据屏幕左半部分,包含所有奇数索引号,一个占据屏幕右半部分,包括所有甚至指数数字)。如果我有奇数个图像,那么最后一个图像会留空。

问题在于,当我有奇数个图像时,我点击最后一个单元格(将容纳两个单元格,但只保留一个单元格),它会将背景颜色更改为白色/非常浅灰色。如何更改此选项以便单击单元格不会更改背景颜色?

请注意以下代码只是一个代码段。我省略了屏幕宽度检测和方向,以保持一切相关。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellIndentifier = @"BasicCell";
UITableViewCell *myCell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
myCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];


CGRect result = [UIScreen mainScreen].bounds;
CGFloat width = CGRectGetWidth(result);
CGFloat height = CGRectGetHeight(result);

UIInterfaceOrientation interfaceOrientation = [UIApplication sharedApplication].statusBarOrientation;
if(UIInterfaceOrientationIsPortrait(interfaceOrientation)){
    result.size = CGSizeMake(width, height);
} else if(UIInterfaceOrientationIsLandscape(interfaceOrientation)) {
    result.size = CGSizeMake(height, width);
}

UIImageView *myImageView;
UIImageView *myImageView2;

myCell.contentView.backgroundColor = [ UIColor colorWithRed:0/255.0f green: 17/255.0f blue:34/255.0f alpha:1];

if(result.size.width <= 480) {
    Companies *item = _feedItems[indexPath.row];

    myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,CGRectGetWidth(self.listTableView.bounds), (item.imageName.size.height / item.imageName.size.width) * CGRectGetWidth(self.listTableView.bounds))];

    myImageView.tag = indexPath.row;
    myImageView.image = item.imageName;
    myImageView.userInteractionEnabled = YES;

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onImageTap:)];
    [tap setNumberOfTouchesRequired:1];
    [tap setNumberOfTapsRequired:1];
    //[tap setDelegate:self];

    [myImageView addGestureRecognizer:tap];

    [myCell addSubview:myImageView];
}
if(result.size.width > 480) {
    NSLog(@"IndexPath.row: %d, feedItems: %d",indexPath.row, _feedItems.count);
    if((indexPath.row*2+1) <= _feedItems.count) {
        Companies *item = _feedItems[indexPath.row * 2];
        myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,CGRectGetWidth(self.listTableView.bounds) / 2, (item.imageName.size.height / item.imageName.size.width) / 2 * CGRectGetWidth(self.listTableView.bounds))];
        myImageView.tag = indexPath.row*2;
        myImageView.image = item.imageName;
        myImageView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onImageTap:)];
        [tap setNumberOfTouchesRequired:1];
        [tap setNumberOfTapsRequired:1];
        [myImageView addGestureRecognizer:tap];
        [myCell addSubview:myImageView];

        if ((indexPath.row*2+1) < _feedItems.count) {
            Companies *item2 = _feedItems[(indexPath.row * 2) + 1];
            myImageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetWidth(self.listTableView.bounds) / 2,0,CGRectGetWidth(self.listTableView.bounds) / 2, (item.imageName.size.height / item.imageName.size.width) / 2 * CGRectGetWidth(self.listTableView.bounds))];
            myImageView2.tag = indexPath.row*2+1;
            myImageView2.image = item2.imageName;
            myImageView2.userInteractionEnabled = YES;
            UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onImageTap:)];
            [tap2 setNumberOfTouchesRequired:1];
            [tap2 setNumberOfTapsRequired:1];
            [myImageView2 addGestureRecognizer:tap2];
            [myCell addSubview:myImageView2];
        } else {
            //myCell.contentView.backgroundColor = [ UIColor colorWithRed:0/255.0f green: 17/255.0f blue:34/255.0f alpha:1];
        }
    }
}

return myCell;
}

仅当第二个图像不存在且用户点击该空白区域时才会发生这种情况。有谁知道我错过了什么,或是什么导致该领域回到白色?在Attributes Inspector中,我将Selection设置为None,希望能做什么。

1 个答案:

答案 0 :(得分:2)

在tableview委托方法cellforrowatindexpath中试试这个

 cell.selectionStyle = UITableViewCellSelectionStyleNone;
相关问题