【问题标题】:UITableView - problem when selectedUITableView - 选择时出现问题
【发布时间】:2011-06-08 00:49:50
【问题描述】:

再次嗨!我在选择单个单元格时遇到问题。如果我的单个单元格被选中 - 我的子视图 UIButton 也会突出显示。该怎么办?

谢谢大家!

这是我的代码:

//
//  ThumbnailsBrowser.m
//  PHOTO_VIDEO
//
//  Created by dev on 07.06.11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#define COUNT 3             //  Total number of elements in a single cell.
#define SPINNER_SIZE 20     //  Size of spinner in a single cell.
#define THUMBNAIL_SIZE 200  //  Size of thumbnail in a single cell.
#define CELL_SIZE 300       //  Size of single cell in a table view.

#import "ThumbnailsBrowser.h"

@implementation ThumbnailsBrowser

- (void)sharedDelegate{
    [self setDelegate:self];
    [self setDataSource:self];
}

- (void)initWithThumbnailsUrl:(NSMutableArray *)url{

}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return CELL_SIZE;
}

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

    static NSString *CellIdentifier = @"Thumbnails";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];

        for(NSInteger i = 0; i < COUNT; i++){

            // Calculate spinner.
            float size_one_section = (self.frame.size.width / COUNT);
            float size_center_object = size_one_section / 2;
            float init_coord_object = size_center_object - (SPINNER_SIZE / 2);        
            float center_object_y = CELL_SIZE / 2 - (SPINNER_SIZE / 2);        
            [cell.contentView addSubview:[self activityIndicatorView:CGRectMake(init_coord_object + i * size_one_section, center_object_y, SPINNER_SIZE, SPINNER_SIZE)]];

            // Calculate thumbnails.
            float init_coord_objectT = size_center_object - (THUMBNAIL_SIZE / 2);        
            float center_object_yT = CELL_SIZE / 2 - (THUMBNAIL_SIZE / 2);        
            [cell.contentView addSubview:[self thumbnailsView:CGRectMake(init_coord_objectT + i * size_one_section, center_object_yT, THUMBNAIL_SIZE, THUMBNAIL_SIZE)]];

        }

        // Set selected cell color. 
        UIView *bgColorView = [[UIView alloc] init];
        [bgColorView setBackgroundColor:[UIColor blackColor]];
        [cell setSelectedBackgroundView:bgColorView];
        [bgColorView release];

    }

    return cell;

}


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

}

//  Create UIActivityIndicatorView and return to cell table view.
- (UIActivityIndicatorView *)activityIndicatorView:(CGRect)frame{    

    UIActivityIndicatorView * spinner =
    [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite] autorelease];
    [spinner setFrame:frame];
    [spinner startAnimating];    
    return spinner;

}

//  Create thumbnails view.
- (UIButton *)thumbnailsView:(CGRect)frame{

    UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [myButton setFrame:frame];
    return myButton;

}

@end

【问题讨论】:

    标签: ios uitableview uibutton cell highlight


    【解决方案1】:
    • (UIButton *)thumbnailsView:(CGRect)frame{

      UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

      [myButton setFrame:frame];

      [myButton addTarget:self action:@selector(urfunctionName) forControlEvents:UIControlEventTouchUpInside];

      返回我的按钮;

    } 在.h中声明这个函数

    -(void)urfunctionName;

    -(void)urfunctionName{

    }

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

    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    
    
        //here call ur buton function
    
    [self urfunctionName];
    

    }

    【讨论】:

    • 谢谢!但是当我选择单元格时,我的 UIButton 也突出显示了。如何调用方法禁用突出显示 UIButton?
    • 将ImageWhenHighlighted 调整为yes
    【解决方案2】:

    有一个属性adjustImageWhenHighlighted 很简单

    “一个布尔值,用于确定按钮突出显示时图像是否发生变化。”

    直接从图书馆。您可以在 selectedRow 方法中将 UIButtonNO 设置为 NO,如果您对该按钮的操作正在被访问,您也可以按照 @madhu 关于设置触摸时设置操作的说明在其中进行设置。

    实际上并没有尝试过,因为通常当我有一个自定义UITableViewCell 时,我只是将其子类化并在该子类中添加所有子视图。

    另外,记得释放分配。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多