这简直让我大吃一惊。
我有一个自定义 UIView 和 UILable 作为子视图添加到 cell.contentView 中的 UITableView 中。我有一个名为 arryData 的数组,其中包含大约 100 个字符串作为我想在表中显示的数据。问题是,当创建表时,我看到 arryData 的前 0-4 行带有值,如果我继续向下滚动表,我看到的只是前 5 个字符串一遍又一遍地重复。我究竟做错了什么?这是我的代码。
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//NSLog(@"Inside cellForRowAtIndexPath");
static NSString *CellIdentifier = @"Cell";
// Try to retrieve from the table view a now-unused cell with the given identifier.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UILabel *labelValue1 = [[UILabel alloc] initWithFrame:CGRectMake(70, 25, 200, 30)];
// If no cell is available, create a new one using the given identifier.
if (cell == nil)
{
// Use the default cell style.
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
//cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
UIImageView *imgVw1 = [[UIImageView alloc] initWithFrame:CGRectMake(11, 0, 300, 75)];
imgVw1.image = [UIImage imageNamed:@"background_pic5.png"];
imgVw1.userInteractionEnabled = YES;
imgVw1.exclusiveTouch = YES;
[cell.contentView addSubview:imgVw1];
labelValue1.text = [arryData objectAtIndex:indexPath.row];
labelValue1.font = [UIFont fontWithName:@"BradleyHandITCTT-Bold" size: 22.0];
labelValue1.textColor = [UIColor whiteColor];
labelValue1.textAlignment = UITextAlignmentCenter;
labelValue1.numberOfLines = 1;
labelValue1.backgroundColor = [UIColor clearColor];
labelValue1.adjustsFontSizeToFitWidth = YES;
labelValue1.minimumFontSize = 10.0;
labelValue1.tag = (indexPath.row)+100;
[cell.contentView addSubview:labelValue1];
NSLog(@"indexPath.row: %d - arrayData: %@..." , indexPath.row, [arryData objectAtIndex:indexPath.row]);
}
else
{
NSLog(@"indexPath.row: %d - arrayData: %@..." , indexPath.row, [arryData objectAtIndex:indexPath.row]);
labelValue1 = (UILabel *) [cell viewWithTag:((indexPath.row)+100)];
labelValue1.text = [arryData objectAtIndex:indexPath.row];
}
//Do this only if row has a value. For blank ones, Skip this
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
{
//its iphone
cell.textLabel.font = [UIFont systemFontOfSize:13];
}
if([arryDataMutable containsObject:indexPath])
{
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
}
else
{
[cell setAccessoryType:UITableViewCellAccessoryNone];
}
//cell.backgroundColor = [UIColor whiteColor];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
这就是当我向下滚动表格时我的输出的样子,我看到的只是我 labelValue1 中 indexPath.row 0 - 4 中的文本一遍又一遍地重复
indexPath.row: 0 - arrayData: Behind Whipped...
indexPath.row: 1 - arrayData: Aftershock Whip...
indexPath.row: 2 - arrayData: Bull Whipped Sound 1...
indexPath.row: 3 - arrayData: Bull Whipped Sound 2...
indexPath.row: 4 - arrayData: Bullet Fire Whip...
indexPath.row: 5 - arrayData: Circus Whip...
indexPath.row: 6 - arrayData: Circus Whip2...