Note that there are some explanatory texts on larger screens.

plurals
  1. POUITableView, strange number of subviews in only one cell
    primarykey
    data
    text
    <p>I've a normal table with some customization in cells, a textview, a textfield, a button and a label. Here the code:</p> <p><strong>header:</strong></p> <pre><code>#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue &amp; 0xFF0000) &gt;&gt; 16))/255.0 green:((float)((rgbValue &amp; 0xFF00) &gt;&gt; 8))/255.0 blue:((float)(rgbValue &amp; 0xFF))/255.0 alpha:1.0] @interface ALCViewController : UIViewController&lt;UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate&gt; { NSMutableArray *prodottiArray; } @property (strong, nonatomic) UITableView *prodotti; @end </code></pre> <p><strong>implementation:</strong></p> <pre><code>@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&lt;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 </code></pre> <p>I'v put button and textfield into a <code>UIView</code> because i need to give to this elements another tag.</p> <p>That's the log taken in <code>- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath</code>. The view tagged with 4 have always 1 subview, but in the fifth row has 3 subviews!!! Why???</p> <p><strong>the Log:</strong></p> <pre><code>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 </code></pre> <p>I'm using Xcode5 and it happens both in simulator (ios7, ios6.1) and device (ios6.1)</p> <p>Any ideas?</p> <p><strong>EDIT 1:</strong></p> <p>As suggested i tried to add views to cell.contentView instead directly in cell, but without changes in log</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload