我有一个包含国家/地区列表的UITableView,用户可以在其中选择多个单元格。但是,当我点击一个单元格时,每个第八个单元格也会在它旁边放置一个复选标记!我不知道为什么会这样,而且只是勾选标记。附加到第八个单元格的代码没有触发,所以它只是一个视觉效果。请帮忙!这是我的表的didSelectRow的代码:
- (void)tableView:(UITableView *)tableView1 didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *selectedCell = [tableView1 cellForRowAtIndexPath:indexPath];
if ([selectedCell accessoryType] == UITableViewCellAccessoryNone) {
[selectedCell setAccessoryType:UITableViewCellAccessoryCheckmark];
int selectedRow = indexPath.row;
NSString *intString = [NSString stringWithFormat:@"%d", selectedRow];
if ([intString isEqualToString: @"0"] ) {
NSString *countriestill = countriesselected;
if ([countriesselected length] == 0) {
countriesselected = [countriestill stringByAppendingString:@"AR"];
}
else {
countriesselected = [countriestill stringByAppendingString:@"-AR"];
}
}
if ([intString isEqualToString: @"1"] ) {
NSString *countriestill = countriesselected;
if ([countriesselected length] == 0) {
countriesselected = [countriestill stringByAppendingString:@"AU"];
}
else {
countriesselected = [countriestill stringByAppendingString:@"-AU"];
}
}
if ([intString isEqualToString: @"2"] ) {
NSString *countriestill = countriesselected;
if ([countriesselected length] == 0) {
countriesselected = [countriestill stringByAppendingString:@"BR"];
}
else {
countriesselected = [countriestill stringByAppendingString:@"-BR"];
}
}
if ([intString isEqualToString: @"3"] ) {
NSString *countriestill = countriesselected;
if ([countriesselected length] == 0) {
countriesselected = [countriestill stringByAppendingString:@"CA"];
}
else {
countriesselected = [countriestill stringByAppendingString:@"-CA"];
}
}
if ([intString isEqualToString: @"4"] ) {
NSString *countriestill = countriesselected;
if ([countriesselected length] == 0) {
countriesselected = [countriestill stringByAppendingString:@"CZ"];
}
else {
countriesselected = [countriestill stringByAppendingString:@"-CZ"];
}
}
if ([intString isEqualToString: @"5"] ) {
NSString *countriestill = countriesselected;
if ([countriesselected length] == 0) {
countriesselected = [countriestill stringByAppendingString:@"FR"];
}
else {
countriesselected = [countriestill stringByAppendingString:@"-FR"];
}
}
if ([intString isEqualToString: @"6"] ) {
NSString *countriestill = countriesselected;
if ([countriesselected length] == 0) {
countriesselected = [countriestill stringByAppendingString:@"DE"];
}
else {
countriesselected = [countriestill stringByAppendingString:@"-DE"];
}
}
if ([intString isEqualToString: @"7"] ) {
NSString *countriestill = countriesselected;
if ([countriesselected length] == 0) {
countriesselected = [countriestill stringByAppendingString:@"GB"];
}
else {
countriesselected = [countriestill stringByAppendingString:@"-GB"];
}
}
if ([intString isEqualToString: @"8"] ) {
NSString *countriestill = countriesselected;
if ([countriesselected length] == 0) {
countriesselected = [countriestill stringByAppendingString:@"HK"];
}
else {
countriesselected = [countriestill stringByAppendingString:@"-HK"];
}
}
if ([intString isEqualToString: @"9"] ) {
NSString *countriestill = countriesselected;
if ([countriesselected length] == 0) {
countriesselected = [countriestill stringByAppendingString:@"IN"];
}
else {
countriesselected = [countriestill stringByAppendingString:@"-IN"];
}
}
if ([intString isEqualToString: @"10"] ) {
NSString *countriestill = countriesselected;
if ([countriesselected length] == 0) {
countriesselected = [countriestill stringByAppendingString:@"IE"];
}
else {
countriesselected = [countriestill stringByAppendingString:@"-IE"];
}
}
if ([intString isEqualToString: @"11"] ) {
NSString *countriestill = countriesselected;
if ([countriesselected length] == 0) {
countriesselected = [countriestill stringByAppendingString:@"IL"];
}
else {
countriesselected = [countriestill stringByAppendingString:@"-IL"];
}
}
if ([intString isEqualToString: @"12"] ) {
NSString *countriestill = countriesselected;
if ([countriesselected length] == 0) {
countriesselected = [countriestill stringByAppendingString:@"IT"];
}
else {
countriesselected = [countriestill stringByAppendingString:@"-IT"];
}
}
if ([intString isEqualToString: @"13"] ) {
NSString *countriestill = countriesselected;
if ([countriesselected length] == 0) {
countriesselected = [countriestill stringByAppendingString:@"JP"];
}
else {
countriesselected = [countriestill stringByAppendingString:@"-JP"];
}
}
if ([intString isEqualToString: @"14"] ) {
NSString *countriestill = countriesselected;
if ([countriesselected length] == 0) {
countriesselected = [countriestill stringByAppendingString:@"MX"];
}
else {
countriesselected = [countriestill stringByAppendingString:@"-MX"];
}
}
if ([intString isEqualToString: @"15"] ) {
NSString *countriestill = countriesselected;
if ([countriesselected length] == 0) {
countriesselected = [countriestill stringByAppendingString:@"NL"];
}
else {
countriesselected = [countriestill stringByAppendingString:@"-NL"];
}
}
if ([intString isEqualToString: @"16"] ) {
NSString *countriestill = countriesselected;
if ([countriesselected length] == 0) {
countriesselected = [countriestill stringByAppendingString:@"NZ"];
}
else {
countriesselected = [countriestill stringByAppendingString:@"-NZ"];
}
}
if ([intString isEqualToString: @"17"] ) {
NSString *countriestill = countriesselected;
if ([countriesselected length] == 0) {
countriesselected = [countriestill stringByAppendingString:@"PL"];
}
else {
countriesselected = [countriestill stringByAppendingString:@"-PL"];
}
}
if ([intString isEqualToString: @"18"] ) {
NSString *countriestill = countriesselected;
if ([countriesselected length] == 0) {
countriesselected = [countriestill stringByAppendingString:@"RU"];
}
else {
countriesselected = [countriestill stringByAppendingString:@"-RU"];
}
}
if ([intString isEqualToString: @"19"] ) {
NSString *countriestill = countriesselected;
if ([countriesselected length] == 0) {
countriesselected = [countriestill stringByAppendingString:@"ZA"];
}
else {
countriesselected = [countriestill stringByAppendingString:@"-ZA"];
}
}
if ([intString isEqualToString: @"20"] ) {
NSString *countriestill = countriesselected;
if ([countriesselected length] == 0) {
countriesselected = [countriestill stringByAppendingString:@"KR"];
}
else {
countriesselected = [countriestill stringByAppendingString:@"-KR"];
}
}
if ([intString isEqualToString: @"21"] ) {
NSString *countriestill = countriesselected;
if ([countriesselected length] == 0) {
countriesselected = [countriestill stringByAppendingString:@"ES"];
}
else {
countriesselected = [countriestill stringByAppendingString:@"-ES"];
}
}
if ([intString isEqualToString: @"22"] ) {
NSString *countriestill = countriesselected;
if ([countriesselected length] == 0) {
countriesselected = [countriestill stringByAppendingString:@"SE"];
}
else {
countriesselected = [countriestill stringByAppendingString:@"-SE"];
}
}
if ([intString isEqualToString: @"23"] ) {
NSString *countriestill = countriesselected;
if ([countriesselected length] == 0) {
countriesselected = [countriestill stringByAppendingString:@"TW"];
}
else {
countriesselected = [countriestill stringByAppendingString:@"-TW"];
}
}
if ([intString isEqualToString: @"24"] ) {
NSString *countriestill = countriesselected;
if ([countriesselected length] == 0) {
countriesselected = [countriestill stringByAppendingString:@"US"];
}
else {
countriesselected = [countriestill stringByAppendingString:@"-US"];
}
}
}
else {
[selectedCell setAccessoryType:UITableViewCellAccessoryNone];
int selectedRow = indexPath.row;
NSString *intString = [NSString stringWithFormat:@"%d", selectedRow];
if ([intString isEqualToString: @"0"] ) {
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"-AR"
withString:@""];
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"AR-"
withString:@""];
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"AR"
withString:@""];
}
if ([intString isEqualToString: @"1"] ) {
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"-AU"
withString:@""];
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"AU-"
withString:@""];
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"AU"
withString:@""];
}
if ([intString isEqualToString: @"2"] ) {
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"-BR"
withString:@""];
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"BR-"
withString:@""];
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"BR"
withString:@""];
}
if ([intString isEqualToString: @"3"] ) {
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"-CA"
withString:@""];
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"CA-"
withString:@""];
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"CA"
withString:@""];
}
if ([intString isEqualToString: @"4"] ) {
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"-CZ"
withString:@""];
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"CZ-"
withString:@""];
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"CZ"
withString:@""];
}
if ([intString isEqualToString: @"5"] ) {
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"-FR"
withString:@""];
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"FR-"
withString:@""];
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"FR"
withString:@""];
}
if ([intString isEqualToString: @"6"] ) {
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"-DE"
withString:@""];
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"DE-"
withString:@""];
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"DE"
withString:@""];
}
if ([intString isEqualToString: @"7"] ) {
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"-GB"
withString:@""];
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"GB-"
withString:@""];
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"GB"
withString:@""];
}
if ([intString isEqualToString: @"8"] ) {
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"-HK"
withString:@""];
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"HK-"
withString:@""];
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"HK"
withString:@""];
}
if ([intString isEqualToString: @"9"] ) {
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"-IN"
withString:@""];
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"IN-"
withString:@""];
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"IN"
withString:@""];
}
if ([intString isEqualToString: @"10"] ) {
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"-IE"
withString:@""];
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"IE-"
withString:@""];
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"IE"
withString:@""];
}
if ([intString isEqualToString: @"11"] ) {
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"-IL"
withString:@""];
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"IL-"
withString:@""];
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"IL"
withString:@""];
}
if ([intString isEqualToString: @"12"] ) {
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"-IT"
withString:@""];
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"IT-"
withString:@""];
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"IT"
withString:@""];
}
if ([intString isEqualToString: @"13"] ) {
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"-JP"
withString:@""];
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"JP-"
withString:@""];
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"JP"
withString:@""];
}
if ([intString isEqualToString: @"14"] ) {
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"-MX"
withString:@""];
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"MX-"
withString:@""];
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"MX"
withString:@""];
}
if ([intString isEqualToString: @"15"] ) {
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"-NL"
withString:@""];
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"NL-"
withString:@""];
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"NL"
withString:@""];
}
if ([intString isEqualToString: @"16"] ) {
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"-NZ"
withString:@""];
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"NZ-"
withString:@""];
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"NZ"
withString:@""];
}
if ([intString isEqualToString: @"17"] ) {
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"-PL"
withString:@""];
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"PL-"
withString:@""];
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"PL"
withString:@""];
}
if ([intString isEqualToString: @"18"] ) {
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"-RU"
withString:@""];
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"RU-"
withString:@""];
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"RU"
withString:@""];
}
if ([intString isEqualToString: @"19"] ) {
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"-ZA"
withString:@""];
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"ZA-"
withString:@""];
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"ZA"
withString:@""];
}
if ([intString isEqualToString: @"20"] ) {
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"-KR"
withString:@""];
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"KR-"
withString:@""];
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"KR"
withString:@""];
}
if ([intString isEqualToString: @"21"] ) {
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"-ES"
withString:@""];
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"ES-"
withString:@""];
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"ES"
withString:@""];
}
if ([intString isEqualToString: @"22"] ) {
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"-SE"
withString:@""];
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"SE-"
withString:@""];
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"SE"
withString:@""];
}
if ([intString isEqualToString: @"23"] ) {
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"-TW"
withString:@""];
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"TW-"
withString:@""];
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"TW"
withString:@""];
}
if ([intString isEqualToString: @"24"] ) {
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"-US"
withString:@""];
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"US-"
withString:@""];
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:@"US"
withString:@""];
}
}
[tableView deselectRowAtIndexPath:indexPath animated:NO];
}
答案 0 :(得分:2)
如果您不熟悉“重构”一词,则意味着(在这种情况下)避免在代码中重复自己。在线有一些关于“DRY编程”的非常好的教程。例如,在不改变程序中的任何其他内容的情况下,下面是大约30行的代码,而不是500多行。除了缩短之外,它还意味着如果你做出改变,你只需要做一次而不是24或48次。最后,它可以更容易地看到你在做什么。
特别是,我同意trydis,你的问题肯定不在这段代码中,而是在你的cellForRowAtIndexPath中。在您从队列中拉出一个单元格或创建它之后,[cell setAccessoryType:UITableViewCellAccessoryNone]否则,当它从屏幕上消失时,出列的单元格将具有该单元格中的任何附件。八个可能来自屏幕上同时有八个单元格,因此被检查的单元格从顶部消失,并在底部“重新出现”。
作为最终评论,实时构建国家代码字符串有点难看(因此关于连字符的所有内容)。可能最好将活动代码列表保存在NSMutableSet中,然后在即将显示时添加连字符。 ([[countriesSelected allObjects] componentsJoinedByString @“ - ”]
NSArray * countryCodes =
[NSArray arrayWithObjects:@"AR",@"AU",@"BR",@"CA",@"CZ",@"FR",@"DE",@"GB",@"HK",@"IN",@"IE",@"IL",@"IT",
@"JP",@"MX",@"NL",@"NZ",@"PL",@"RU",@"ZA",:@"KR",@"ES",@"SE",@"TW",@"US",nil];
- (void)tableView:(UITableView *)tableView1 didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *selectedCell = [tableView1 cellForRowAtIndexPath:indexPath];
NSString *countryCode = [countryCodes objectAtIndex:indexPath.row];
if ([selectedCell accessoryType] == UITableViewCellAccessoryNone) {
[selectedCell setAccessoryType:UITableViewCellAccessoryCheckmark];
if ([countriesselected length] == 0) {
countriesselected = countryCode;
} else {
countriesselected = [NSString stringWithFormat:@"%@-%@",countriesselected,countryCode]]; // note change to include hyphen
}
} else {
[selectedCell setAccessoryType:UITableViewCellAccessoryNone];
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"-%@",countryCode]
withString:@""];
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@-",countryCode]
withString:@""];
countriesselected = [countriesselected stringByReplacingOccurrencesOfString:countryCode"
withString:@""];
}
[tableView deselectRowAtIndexPath:indexPath animated:NO];
用于cellForRowAtIndePath的代码:
NSString *countryCode = [countryCodes objectAtIndex:indexPath.row];
if ([countriesselected rangeOfString: countryCode].location != NSNotFound) {
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
} else {
[cell setAccessoryType:UITableViewCellAccessoryNone];
}
答案 1 :(得分:1)
代码迫切需要进行一些重构,但我会想到这个问题是由细胞重用引起的。您是否在cellForRowAtIndexPath方法中使用dequeueReusableCellWithIdentifier?如果是这样,请记住每次调用时都设置正确的accessoryType,而不是在创建单元格时。