セル、テキストビュー、テキストフィールド、ボタン、ラベルをカスタマイズした通常のテーブルがあります。ここにコード:
ヘッダ:
#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) の両方で発生します。
何か案は?
編集1:
提案されているように、セルに直接ビューを追加しようとしましたが、ログに変更はありませんでした