我正在尝试创建自定义表格单元格,uitableview有4行,但它会创建2个单元格。
我在cellForRowAtIndexPath方法中动态添加视图。它添加了第一行和最后一行。
我做错了什么?
我的代码如下
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// NSLog(@"cellForRowAtIndexPath");
//creating table rows
static NSString *CellIdentifier = @"TableCell";
STGTableViewCell *cell = (STGTableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//NSMutableDictionary * temp = [((STGTableView*)tableView).tableDataSource objectAtIndex:[indexPath row]];
if (cell == nil) {
//cell.globalView = ((STGTableView*)tableView).cellView;
cell = [[STGTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
//[[STGTableViewCell alloc] initWithWiew:((STGTableView*)tableView).cellView];
//cell = ((STGTableView*)tableView).globalCell;
NSLog(@"((STGTableView*)tableView).globalCell rowno:%d\n",[indexPath row]);
UIView * tempCell = ((STGTableView*)tableView).cellView;
for(UIView *sview in [tempCell subviews])
{
if([sview isKindOfClass:[UIScrollView class]])
{
[cell.contentView addSubview:sview];
}
}
NSLog(@"inside alloc");
}
NSLog(@"outside alloc");
for(UIView *sview in [cell.contentView subviews])
{
if([sview isKindOfClass:[UIScrollView class]])
{
for(UIView *subview in [sview subviews])
{
if([subview isKindOfClass:[UILabel class]])
{
NSMutableDictionary *tempDict = [((STGTableView*)tableView).tableDataSource objectAtIndex:[indexPath row]];
NSLog(@"%d",[indexPath row]);
NSLog(@"%@",((UILabel*)subview).text);
if([((UILabel*)subview).text isKindOfClass:[NSString class]] &&
![((UILabel*)subview).text isKindOfClass:[NSNumber class]])
{
NSRange range = [((UILabel*)subview).text rangeOfString:@"#"];
if (range.location != NSNotFound)
{
if([[((UILabel*)subview).text substringToIndex:1] isEqualToString:@"#"])
{
NSLog(@"%@",((UILabel*)subview).text);
NSString *tempKey = [((UILabel*)subview).text stringByReplacingOccurrencesOfString:@"#" withString:@""];
[((UILabel*)subview) setText:[NSString stringWithFormat:@"%@",[tempDict objectForKey:tempKey]]];
}
}
}
}
else
{
}
}
}
}
return cell;
}
答案 0 :(得分:0)
您第一次将滚动视图添加到内容视图。好像(cell == nil)条件最初调用一次。修改您的代码如下...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// NSLog(@"cellForRowAtIndexPath");
//creating table rows
static NSString *CellIdentifier = @"TableCell";
STGTableViewCell *cell = (STGTableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//NSMutableDictionary * temp = [((STGTableView*)tableView).tableDataSource objectAtIndex:[indexPath row]];
if (cell == nil)
{
//cell.globalView = ((STGTableView*)tableView).cellView;
cell = [[STGTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
//[[STGTableViewCell alloc] initWithWiew:((STGTableView*)tableView).cellView];
//cell = ((STGTableView*)tableView).globalCell;
NSLog(@"((STGTableView*)tableView).globalCell rowno:%d\n",[indexPath row]);
NSLog(@"inside alloc");
}
NSLog(@"outside alloc");
UIView * tempCell = ((STGTableView*)tableView).cellView;
for(UIView *sview in [tempCell subviews])
{
if([sview isKindOfClass:[UIScrollView class]])
{
[cell.contentView addSubview:sview];
}
}
for(UIView *sview in [cell.contentView subviews])
{
if([sview isKindOfClass:[UIScrollView class]])
{
for(UIView *subview in [sview subviews])
{
if([subview isKindOfClass:[UILabel class]])
{
NSMutableDictionary *tempDict = [((STGTableView*)tableView).tableDataSource objectAtIndex:[indexPath row]];
NSLog(@"%d",[indexPath row]);
NSLog(@"%@",((UILabel*)subview).text);
if([((UILabel*)subview).text isKindOfClass:[NSString class]] &&
![((UILabel*)subview).text isKindOfClass:[NSNumber class]])
{
NSRange range = [((UILabel*)subview).text rangeOfString:@"#"];
if (range.location != NSNotFound)
{
if([[((UILabel*)subview).text substringToIndex:1] isEqualToString:@"#"])
{
NSLog(@"%@",((UILabel*)subview).text);
NSString *tempKey = [((UILabel*)subview).text stringByReplacingOccurrencesOfString:@"#" withString:@""];
[((UILabel*)subview) setText:[NSString stringWithFormat:@"%@",[tempDict objectForKey:tempKey]]];
}
}
}
}
else
{
}
}
}
}
return cell;
}
希望它可以帮助你......!