我有一个普通的表,在单元格中有一些自定义,文本视图,文本字段,按钮和标签。代码如下:
标题
#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个子视图,但在第五行中有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,但没有更改日志
答案 0 :(得分:0)
我遇到了同样的情况。但我有一个解决方案。我刚刚更改了超过1000+的标签,它对我来说非常适合。当我搜索这个时,我得到了一个理由,原因是一些标签已经是系统使用的服务器。可以帮助你。试试吧。