想要动态创建一个 uitableview,在一个部分中有 2 行
我写了这段代码,但我有问题,在所有部分中只重复 2 首行
现在我有这一行 0,1 -> section 0 , row 0,1 -> section 1 , row 0,1 -> section 2
想要在例如第 0,1 行 -> 第 0 节、第 2,3 行 -> 第 1 节、第 4,5 行 -> 第 2 节 -> 第 6,7 行之后读取所有行 items = [[NSMutableArray alloc] init];
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{ return items.count/2; }
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{ return 2; }
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
UILabel *label = nil;
cell = [tv dequeueReusableCellWithIdentifier:@"Cell"];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"Cell"] autorelease];
label = [[UILabel alloc] initWithFrame:CGRectZero];
[label setLineBreakMode:UILineBreakModeWordWrap];
[label setMinimumFontSize:FONT_SIZE];
[label setNumberOfLines:0];
[label setFont:[UIFont systemFontOfSize:FONT_SIZE]];
[label setTag:1];
label.textColor = [UIColor darkGrayColor];
[label setAlpha:0.8];
cell.backgroundColor = [UIColor whiteColor];
[[cell contentView] addSubview:label];
}
NSString *text = [items objectAtIndex:[indexPath row]];
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
if (!label)
label = (UILabel*)[cell viewWithTag:1];
[label setText:text];
[label setFrame:CGRectMake(CELL_CONTENT_MARGIN-5, CELL_CONTENT_MARGIN+30, CELL_CONTENT_WIDTH - ((CELL_CONTENT_MARGIN * 2)+6), MAX(size.height, 44.0f))];
label.backgroundColor = [UIColor clearColor];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
for (UIView *theSubview in [cell.contentView subviews])
{
if([theSubview isKindOfClass:[UIImageView class]])
{
[theSubview removeFromSuperview];
}
}
UIImageView *imv = [[UIImageView alloc]initWithFrame:CGRectMake(128,5, 32, 32)];
imv.image=[UIImage imageNamed:@"0England.png"];
[cell.contentView addSubview:imv];
[imv release];
if (indexPath.row % 2==0) {
UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(-7 ,10, 8, 18)];
img.image=[UIImage imageNamed:@"...."];
[cell.contentView addSubview:img];
[img release];
}
else { }
return cell;
}