[英]UITableView Section condition not working
我的表格視圖中有一個非常奇怪的問題。
我在表視圖中使用了不同的部分,每個部分具有不同的行數。 我在表格視圖的單元格中有一些文本字段。 問題是,當我向下滾動表格視圖時,第一節的文本字段的第一個單元格與最后一節的第一行文本字段相互重疊。 當我向上滾動表格視圖時,也會發生同樣的事情。
static NSString *cellIdetifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdetifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdetifier];
}
cell.detailTextLabel.textColor = [UIColor blackColor];
if(indexPath.section == 0)
{
if(indexPath.row == 0)
{
titleAdded = 1;
self.nameView.backgroundColor = [UIColor clearColor];
self.eventName.textColor = [UIColor colorWithRed:128.0/255.0 green:128.0/255.0 blue:128.0/255.0 alpha:1.0];
[cell addSubview:self.nameView];
}
else if (indexPath.row == 1)
{
NSLog(@"in section 0");
self.locationView.backgroundColor = [UIColor clearColor];
self.locationField.textColor = [UIColor colorWithRed:128.0/255.0 green:128.0/255.0 blue:128.0/255.0 alpha:1.0];
[cell addSubview:self.locationView];
}
}
else if (indexPath.section == 1)
{
if(indexPath.row == 0)
{
cell.textLabel.text = NSLocalizedString(@"Starts", @"");
if(self.selectedDate != nil)
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
//[formatter setDateFormat:@"yyyy-mm-dd"];
[formatter setDateStyle:NSDateFormatterFullStyle];
NSString *stringFromDate = [formatter stringFromDate:self.selectedDate];
cell.detailTextLabel.text = stringFromDate;
}
}
else if(indexPath.row == 1)
{
cell.textLabel.text = NSLocalizedString(@"Ends", @"");
if(self.selectedDate != nil)
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
//[formatter setDateFormat:@"yyyy-mm-dd"];
[formatter setTimeStyle:NSDateFormatterShortStyle];
NSString *stringFromDate = [formatter stringFromDate:self.selectedTime];
cell.detailTextLabel.text = stringFromDate;
}
}
}
else if (indexPath.section == 2)
{
if(indexPath.row == 0)
{
cell.textLabel.text = NSLocalizedString(@"Repeat",@"");
cell.detailTextLabel.text = NSLocalizedString(@"Never", @"");
}
}
else if (indexPath.section == 3)
{
if(indexPath.row == 0)
{
cell.textLabel.text = NSLocalizedString(@"Alert", @"");
if(![[[NSUserDefaults standardUserDefaults] valueForKey:@"ringtone"] isEqualToString:@""] && [[NSUserDefaults standardUserDefaults] valueForKey:@"ringtone"] != nil)
{
cell.detailTextLabel.text = [[NSUserDefaults standardUserDefaults] valueForKey:@"ringtone"];
}
else
{
cell.detailTextLabel.text = NSLocalizedString(@"None", @"");
}
}
}
else if (indexPath.section == 4)
{
if(indexPath.row == 0)
{
cell.textLabel.text = NSLocalizedString(@"Calendar", @"");
cell.detailTextLabel.text = NSLocalizedString(@"Home", @"");
}
}
else if(indexPath.section == 5 && indexPath.section != 0)
{
if(indexPath.row == 0)
{
self.urlView.backgroundColor = [UIColor clearColor];
self.urlField.textColor = [UIColor colorWithRed:128.0/255.0 green:128.0/255.0 blue:128.0/255.0 alpha:1.0];
[cell addSubview:self.urlView];
}
}
這是我的方法cellforRowatIndexPath
代碼
任何解決方法。 我已經調試了它,當它出現在第5節if語句中時,它找不到該節的值,並且直接跳到了if語句中。 然后出現問題。 我還檢查了它是否為零,但沒有用。
問題是您對每種類型的單元格都使用相同的重用單元格(或相同批次的重用單元格),並且每次重用時都添加了子視圖,而沒有將其刪除。 您應該為每種類型的單元格使用不同的單元格標識符,並且僅在初始化中添加子視圖。
編輯:同樣,您應該將子視圖添加到單元格的contentView中,而不是直接添加到它。
放置[cell addSubview:whatever]
,替換為[cell.contentView addSubview:whatever]
根據馬特對此答案的第一條評論,這是一種實現此問題的快捷方法:
為了刪除單元格的所有子視圖:
for (UIView *view in cell.contentView.subviews)
{
[view removeFromSuperview];
}
將其放在代碼中此行之前
cell.detailTextLabel.textColor = [UIColor blackColor];
請記住,此解決方案在包含許多單元格的表中效果不佳。 您實際上應該將每個單元格拆分為自己的類型,在其中可以使用它們的tag
屬性來跟蹤任何自定義子視圖。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.