2011-06-07 81 views
1

再次嗨!我在選擇單個單元時遇到問題。如果我的單個單元格被選中 - 我的子視圖UIButton也被突出顯示。該怎麼辦?UITableView - 選擇時出現問題

謝謝大家!

此我的代碼:

// 
// 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 

回答

1
  • (的UIButton *)thumbnailsView:(的CGRect)幀{

    的UIButton * myButton的= [UIButton的buttonWithType:UIButtonTypeRoundedRect];

    [myButton setFrame:frame];

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

    return myButton;

} 聲明此功能中.H

- (無效)urfunctionName;

- (無效)urfunctionName {

}

- (無效)的tableView:(UITableView的*)的tableView didSelectRowAtIndexPath方法:(NSIndexPath *)indexPath {

[tableView deselectRowAtIndexPath:indexPath animated:NO]; 



    //here call ur buton function 

[self urfunctionName]; 

}

+0

謝謝!但是當我選擇單元格時,我的UIButton也突出顯示。如何調用方法禁用突出顯示UIButton? – 2011-06-07 15:48:17

+0

adjustImageWhen顯示爲yes – 2011-06-08 08:46:38

1

有一個屬性adjustImageWhenHighlighted這簡直就是

「一個布爾值,用於確定當按鈕高亮顯示時圖像是否更改。」

直接從圖書館。您可以在您的selectedRow方法中將您的UIButton設置爲NO,如果您正在訪問該按鈕的動作,您可以在@ madhu的設置動作時設置該動作。

實際上並沒有嘗試過它,因爲通常當我有一個自定義的UITableViewCell時,我只是將其子類化並在該子類中添加所有子視圖。

此外,請記住釋放分配。