0
我正在使用UITableView。我以編程方式在視圖中添加了一個表格,並通過將UILable作爲每個單元格的子視圖添加了一些數據。表有21行。問題是,當我向上或向下滾動時,前兩行和後兩行或三行中的數據與其他一些行的數據重疊。例如,前兩個擊球手數據與前兩個擊球手數據重疊。任務是首先顯示一個擊球手列表,然後將2個單元格留空,然後在同一個表中顯示一列擊球手列表。我的代碼是UITableView文本重疊
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
NSString *string1;
NSString *string2;
if(indexPath.row < [inningObject.battingLine count]){
Batter *cBatter = [inningObject.battingLine objectAtIndex:indexPath.row];
string1 = cBatter.batterName;
string2 = cBatter.runs;
}else if(indexPath.row > [inningObject.battingLine count]+1){
Bowler *cBowler = [inningObject.bowlingLine objectAtIndex: indexPath.row-[inningObject.battingLine count]-2]; // start from zero for next NSArray
string1 = cBowler.bowlerName;
string2 = cBowler.wickets;
}
CGRect a=CGRectMake(5, 0, 320, 38);
if(indexPath.row < [inningObject.battingLine count] || indexPath.row >= [inningObject.battingLine count]+2){
CGRect string1Rect = CGRectMake(5, 10, 150, 18);
UILabel *string1Label = [[UILabel alloc] initWithFrame:string1Rect];
string1Label.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];
string1Label.textAlignment = UITextAlignmentLeft;
string1Label.text = string1;
string1Label.font = [UIFont boldSystemFontOfSize:17];
[cell.contentView addSubview: string1Label];
[string1Label release];
CGRect string2Rect = CGRectMake(240, 10, 70, 18);
UILabel *string2Label = [[UILabel alloc] initWithFrame:string2Rect];
string2Label.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];
string2Label.textAlignment = UITextAlignmentLeft;
string2Label.text = string2;
string2Label.font = [UIFont boldSystemFontOfSize:17];
[cell.contentView addSubview: string2Label];
[string2Label release];
}
return cell;
}
非常感謝答覆多米諾骨牌。我正在與你推薦的第二個選項。 :-) – Aqueel 2010-08-11 10:05:04