我有一个普通表格,在单元格、文本视图、文本字段、按钮和标签中进行了一些自定义。这里的代码:
标题:
#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
我将按钮和文本字段放入 aUIView
中,因为我需要给这个元素另一个标签。
那是录入的日志- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
。标记为 4 的视图始终有 1 个子视图,但在第五行有 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:
正如建议的那样,我尝试将视图添加到 cell.contentView 而不是直接在单元格中,但日志中没有更改