我有一个带有以下代码的UITableView:
- (UITableViewCell *)tableView:(UITableView *)tableViews cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"MyCell";
TableViewCell *cell = [tableViews dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[TableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
if (indexPath.row == 0){
cell.image.image = [UIImage imageNamed:@"male80.png"];
cell.text.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"Phone" attributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]}];
}
if(indexPath.row == 1) {
cell.image.image = [UIImage imageNamed:@"male80.png"];
cell.text.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"Mobile Phone" attributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]}];
}
if (indexPath.row == 2) {
cell.image.image = [UIImage imageNamed:@"gift41.png"];
cell.text.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"E-mail" attributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]}];
}
if (indexPath.row == 3) {
cell.image.image = [UIImage imageNamed:@"gender.png"];
cell.text.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"address" attributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]}];
}
if (indexPath.row == 4) {
cell.image.image = [UIImage imageNamed:@"gender.png"];
cell.text.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"country" attributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]}];
}
if (indexPath.row == 5) {
cell.image.image = [UIImage imageNamed:@"gender.png"];
cell.text.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"city" attributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]}];
}
if (indexPath.row == 6) {
cell.image.hidden = YES;
cell.text.hidden = YES;
UITextView *view = [[UITextView alloc] initWithFrame:CGRectMake(15, 10, cell.frame.size.width - 18, cell.frame.size.height - 15)];
view.text = @"Text Example";
view.textColor = [UIColor whiteColor];
view.backgroundColor = [UIColor clearColor];
view.tag = 13;
view.layer.borderWidth = 0.5f;
view.layer.cornerRadius = 4;
view.layer.borderColor = [[UIColor grayColor] CGColor];
[cell addSubview:view];
}
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row != 6) {
return 65;
}else{
return 204;
}
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 7;
}
这段代码很简单,他的问题是:
问题是我的表,在0,1和2等行中创建UITextview。但为什么会这样呢? 在我的代码中我说清楚了!只有第6行才能创建UITextView!
我该如何解决这个问题?
答案 0 :(得分:1)
两个问题:
1)您的单元格被回收,这意味着当单元格到达第6行时,您的UITextView被添加到单元格中,然后当向后滚动时,UITextView仍然存在
2)你应该在其contentView中添加cell的子视图,而不是单元本身。
解决方案:
使用两种UITableViewCell,一种特定于第6行,另一种特定于其他行。注册单元格然后将它们出列以获取相应的indexPath。
答案 1 :(得分:0)
我认为您的问题是将NIB文件加载到表格视图中。 查看Assertion failure in dequeueReusableCellWithIdentifier:forIndexPath:
我试图通过使用名为ItemCell
的Interface Builder创建UITableViewCell来重新创建您的问题。确保在viewDidLoad
[super viewDidLoad];
//Load the NIB file
UINib *nib = [UINib nibWithNibName:@"ItemCell"
bundle:nil];
//Register this NIB, which contains the cell
[self.tableView registerNib:nib
forCellReuseIdentifier:@"ItemCell"];
然后替换
static NSString *CellIdentifier = @"MyCell";
TableViewCell *cell = [tableViews dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[TableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
与
ItemCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ItemCell"
forIndexPath:indexPath];
if (cell == nil) {
cell = [[ItemCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"ItemCell"];
}
您可能还希望将图像放在数据结构中以配合MVC设计模式。
答案 2 :(得分:-2)
为表格第6行提供与其他人不同的标识符
static NSString *CellIdentifier = @"MyCell";
if (indexPath.row == 6){
CellIdentifier = @"MyCell6";
}
即使这样,每次调用cellForRowAtIndexPath时都不应该重新分配textField。这是重用细胞的重点!但这是另一个对话,所以请查看如何正确回收细胞
答案 3 :(得分:-2)
将此代码放在viewDidLoad中,让您查看全局变量。
(SELECT *
FROM personTable
WHERE ID IN
( SELECT ID
FROM workerTable
)
AND firstName LIKE 'O%');
(SELECT *
FROM ownsTable
WHERE PhoneNumberID IN
( SELECT ID
FROM phonenumberTable
WHERE Home <>'' AND `Work` <>'' AND Cell <>''
)
);
现在像这样创建tableView:
UITextView *view = [[UITextView alloc] initWithFrame:CGRectMake(15, 10, cell.frame.size.width - 18, cell.frame.size.height - 15)];
view.text = @"Text Example";
view.textColor = [UIColor whiteColor];
view.backgroundColor = [UIColor clearColor];
view.tag = 13;
view.layer.borderWidth = 0.5f;
view.layer.cornerRadius = 4;
view.layer.borderColor = [[UIColor grayColor] CGColor];
}