2012-07-25 94 views
1

我在可用視圖中有30行,我試圖選擇多行並將它們設置爲檢查。UITableView多選。自動選擇行隨機選擇行

每當我選擇一行時,會自動選擇另一行並檢查所需的行。

另外,當我滾動tableview它會自動改變選擇。

我已經使用此代碼行數較少(8),它的工作完美。對於超過12行,它提供了我所描述的問題。

如果你可以建議任何其他代碼/教程,使其工作也沒關係。

我是xcode的新手,非常感謝任何幫助。謝謝。

這裏是我的代碼:

#pragma mark - 
#pragma mark Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
// Return the number of sections. 
return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
// Return the number of rows in the section. 
return [listOfItems count]; 
} 

// Customize the appearance of table view cells. 
- (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]; 
} 

// Configure the cell... 
NSString *cellValue = [listOfItems objectAtIndex:indexPath.row]; 
cell.text = cellValue; 

return cell; 
} 

#pragma mark - 
#pragma mark Table view delegate 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath]; 

if ([selectedCell accessoryType] == UITableViewCellAccessoryNone) { 

    if(count < 3) 
    { 

     [selectedCell setAccessoryType:UITableViewCellAccessoryCheckmark]; 
     [selectedIndexes addObject:[NSNumber numberWithInt:indexPath.row]]; 
     count++; 
    } 

    NSLog(@"Count: %d", count); 
    NSLog(@"Items I selected @ %d:", indexPath.row); 
    NSLog(@"Items I selected: %@", selectedIndexes); 
} 

else { 

    [selectedCell setAccessoryType:UITableViewCellAccessoryNone]; 
    [selectedIndexes removeObject:[NSNumber numberWithInt:indexPath.row]]; 
    count --; 
    NSLog(@"Items I de-selected @ %d:", indexPath.row); 

} 

[tableView deselectRowAtIndexPath:indexPath animated:NO]; 

NSMutableString *resultlearning = [[NSMutableString alloc]init]; 
for (int i = 0; i < [listOfItems count]; i++) { 
    NSIndexPath *path = [NSIndexPath indexPathForRow:i inSection:0]; 
    //[tableView scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionMiddle animated:NO]; 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:path]; 
    if (cell.accessoryType == UITableViewCellAccessoryCheckmark) { 
     [resultlearning appendFormat:@"%@, ",cell.textLabel.text]; 
    } 
} 
if (resultlearning.length > 2) { 
    [resultlearning replaceCharactersInRange:NSMakeRange(resultlearning.length-1, 1) withString:@""]; 
} 
NSLog(@"Result: %@",resultlearning); 
} 

回答

1

在.h文件中添加一個可變陣列。這裏添加與tableDataSourc數組一樣多的對象。現在,我的意圖是,如果它包含0,那麼不選擇該單元格或則包含1該小區選擇

現在這樣做,因爲ü有selectedIndexes陣列所以需要另一mutablearray的:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
// Return the number of rows in the section. 
for(int i = 0;i<[listOfItems count];i++) 
{ 
[selectedIndexes addObject:@"0"]; 
} 
    return [listOfItems count] 
} 


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath]; 

if ([selectedCell accessoryType] == UITableViewCellAccessoryNone) { 

    [selectedCell setAccessoryType:UITableViewCellAccessoryCheckmark]; 
     [selectedIndexes replaceObjectAtIndex:indexPath.row withObject:@"1"]; 
} 
else 
{           
selectedCell.accessoryType = UITableViewCellAccessoryNone; 
[selectedIndexes replaceObjectAtIndex:indexPath.row withObject:@"0"]; 
} 

現在selectedIndexes具有1值的對象意味着該值在listOfItems中被選中

+0

嗨,所以selectedIndexes是你希望我聲明的另一個可變數組,還是應該添加一個比selectedIndexes更多的數組? – 2012-07-25 13:28:15

+0

你不需要添加任何其他可變數組,因爲我假設你有selectedIndexes數組,如果沒有,然後添加可變數組和alloc在viewDidLoad方法 – 2012-07-26 04:36:00

0

嗯...我仍然在今天上午的咖啡我的第一杯,但東西告訴我你已經有行「選擇」時,didSelectRowAtIndexPath方法被調用。然後,似乎if語句可能會做些時髦的事情,並選擇下一行或其他東西。你有沒有試過評論if語句?我知道可能一次只能選一個,但只是爲了看看是否是原因。另外,是否是之前選擇的下一行?

+0

我試過評論。沒有預選的行。沒有它不是以前選擇的行。另外,當我向下滾動或向上滾動時,選擇會自動更改,然後返回以查看選擇。我認爲我的邏輯存在一個重大缺陷。如果你可以建議一些其他的邏輯或代碼也可以。 我想要做的是,在我的表格中有30行,並且能夠從30中選擇最多3行,並將檢查應用於所選行並將檢查行存儲在一個字符串變量中。 – 2012-07-25 12:19:02

2

單元格被緩存和重用,因此如果您將附件設置爲一個,然後將其用於表格中的其他位置,它仍然具有附件。

最好只跟蹤在didSelectRowAtIndexPath:方法中選擇哪些單元格作爲數據,然後打開或關閉cellForRowAtIndexPath:中所有單元格的附件。

+0

好的,你的意思是我應該將索引存儲在一個數組中,然後使用該數組將附件更改爲打開或關閉?你能證明使用代碼塊嗎? – 2012-07-25 12:20:55

+0

或者如果你可以在我的帖子中編輯它會很棒。謝謝你,對於麻煩抱歉,我是xcode的新手。 – 2012-07-25 12:31:33

2

單元格的檢查/取消選中附件類型的邏輯應轉到cellForRowAtindexPath。在didSelectRowAtIndexPath中,您只應該照顧選定的行。