2013-10-23 67 views
0

我有一個正常的表格,在單元格,文本框,文本框,按鈕和標籤中進行了一些自定義。下面的代碼:UITableView,在一個單元格中的奇怪數量的子視圖

頭:

#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0] 

@interface ALCViewController : UIViewController<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate> { 
    NSMutableArray *prodottiArray; 
} 

@property (strong, nonatomic) UITableView *prodotti; 

@end 

實現:

@implementation ALCViewController 

#pragma mark - UITableViewDelegate 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [prodottiArray count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = nil; 

    static NSString *AutoCompleteRowIdentifier = @"AutoCompleteRowIdentifier"; 

    cell = [tableView dequeueReusableCellWithIdentifier:AutoCompleteRowIdentifier]; 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
             reuseIdentifier:AutoCompleteRowIdentifier]; 

     UITextView *productname = [[UITextView alloc] initWithFrame:CGRectMake(115, 5, 100, 105)]; 
     [cell addSubview:productname]; 
     [productname setTag:1];/**/ 

     UILabel *price = [[UILabel alloc] initWithFrame:CGRectMake(230, 5, 85, 26)]; 
     [cell addSubview:price]; 
     [price setTag:2]; 

     UIView *buttonView = [[UIView alloc] initWithFrame:CGRectMake(235, 40, 45, 56)]; 
     UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     [button setFrame:CGRectMake(0, 0, 45, 56)]; 
     [buttonView addSubview:button]; 
     [cell addSubview:buttonView]; 
     [buttonView setTag:3]; 

     UIView *textfieldView = [[UIView alloc] initWithFrame:CGRectMake(235, 110, 45, 30)]; 
     UITextField *textfield = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 45, 30)]; 
     [textfield setTextColor:[UIColor blackColor]]; 
     [textfield setKeyboardType:UIKeyboardTypeNumbersAndPunctuation]; 
     [textfield setReturnKeyType:UIReturnKeyDone]; 
     [textfieldView addSubview:textfield]; 
     [cell addSubview:textfieldView]; 
     [textfieldView setTag:4]; 
    } 

    NSDictionary *elemento = [[NSDictionary alloc] initWithDictionary:[prodottiArray objectAtIndex:indexPath.row]]; 

    [(UITextView *)[cell viewWithTag:1] setText:[elemento objectForKey:@"product_name"]]; 
    [(UILabel *)[cell viewWithTag:2] setText:[elemento objectForKey:@"price"]]; 
    [[[(UIView *)[cell viewWithTag:3] subviews] objectAtIndex:0] setTag:[indexPath item]]; 

    NSLog(@"[(UIView *)[cell viewWithTag:4] subviews] %d",[[(UIView *)[cell viewWithTag:4] subviews] count]); 

    return cell; 
} 

#pragma mark - UITableViewDataSource 

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

} 

#pragma mark - INIT 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    #pragma mark - inizializzazione prodotti 

    prodottiArray = [[NSMutableArray alloc] init]; 

    for (int i=0; i<20; i++) { 
     [prodottiArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"12",@"price",@"This is a name",@"product_name", nil]]; 
    } 

    #pragma mark - Costruzione tabella 

    self.prodotti = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height-30) style:UITableViewStylePlain]; 
    [self.prodotti setDelegate:self]; 
    [self.prodotti setDataSource:self]; 
    [self.prodotti setScrollEnabled:YES]; 
    [self.prodotti setHidden:NO]; 
    [self.prodotti setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]]; 
    [self.prodotti setTag:0]; 

    [self.prodotti setRowHeight:150]; 
    [self.prodotti setBackgroundColor:[UIColor clearColor]]; 

    [self.view addSubview:self.prodotti]; 
    #pragma mark 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 


@end 

心中已經把按鈕和文本框爲UIView,因爲我需要給這個元素另一個標籤。

這是在- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath中記錄的日誌。標有4的視圖總是有1個子視圖,但在第5行有3個子視圖!爲什麼???

日誌:

2013-10-23 11:23:27.660 tableTest[2703:c07] [(UIView *)[cell viewWithTag:4] subviews] 1 
2013-10-23 11:23:29.478 tableTest[2703:c07] [(UIView *)[cell viewWithTag:4] subviews] 3 
2013-10-23 11:23:31.814 tableTest[2703:c07] [(UIView *)[cell viewWithTag:4] subviews] 1 
2013-10-23 11:23:33.415 tableTest[2703:c07] [(UIView *)[cell viewWithTag:4] subviews] 1 
2013-10-23 11:23:34.148 tableTest[2703:c07] [(UIView *)[cell viewWithTag:4] subviews] 1 
2013-10-23 11:23:36.651 tableTest[2703:c07] [(UIView *)[cell viewWithTag:4] subviews] 1 

我使用Xcode5,它可以同時在模擬器(ios7,ios6.1)和設備(ios6.1)

任何想法?

EDIT 1:

如所建議的,我試圖添加意見,而不是直接在細胞cell.contentView,但沒有在更改日誌

+0

嘗試將這些視圖(文本字段,標籤和按鈕視圖)添加到單元格的「contentView」,而不是直接添加到它。這有什麼區別嗎? 'UITableView'實際上有很多魔法,所以很難理解內省的結果。 –

+0

Thankyou Guy Kogus但沒有結果:( – Lucabro

+1

也許你應該試試1000+以上的標籤,因爲有些標籤是爲系統使用保留的... – D33pN16h7

回答

0

我面臨相同situation.But我有個解決方案,這。我剛剛更換了超過1000多個標籤,它對我來說非常完美。當我搜索這個,然後我得到了1個原因,原因是一些標籤已經爲系統use.May它可以幫助你。試試吧。

+0

D33pN16h7已經建議它,無論如何tankhuou – Lucabro

+1

:)我剛分享我的經驗。 – Smita

相關問題