[英]cellForRowAtIndexPath not being called on tableView reloadData
[英]tableView reloadData not working: cellForRowAtIndexPath not called
我有這個問題cellForRowAtIndexPath
不被reloadingData
調用。 我花了很多時間。 我xib文件dataSource
和delegate
連接到File's Owner
。 歡迎任何想法。
這是我的代碼:
* cellForRowAtIndexPath *
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
//cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.backgroundColor = [UIColor clearColor];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
cell.backgroundView.opaque = NO;
//cell.alpha = 0.65;
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.opaque = NO;
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.highlightedTextColor = [UIColor whiteColor];
cell.textLabel.font = [UIFont boldSystemFontOfSize:18];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.opaque = NO;
cell.detailTextLabel.textColor = [UIColor whiteColor];
cell.detailTextLabel.highlightedTextColor = [UIColor whiteColor];
cell.detailTextLabel.font = [UIFont systemFontOfSize:14];
NSString *temp = [distanceArray objectAtIndex:indexPath.row];
if([checkMarkArray count] != 0){
NSString *checkString = [checkMarkArray objectAtIndex:0];
NSLog(@" checkString %@",checkString);
if ([temp isEqualToString:checkString ]) {
cell.accessoryView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"checked.png"]] autorelease];
}
else
{
cell.accessoryView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"unchecked.png"]] autorelease];
}
}
}
// Set up the cell...
[[cell textLabel] setText: [distanceArray objectAtIndex:indexPath.row]] ;
return cell;
}
didSelectRowAtIndexPath * ***
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
cellText = selectedCell.textLabel.text;
NSLog(@"%@",cellText);
[checkMarkArray removeAllObjects];
if([[tableViewDistance cellForRowAtIndexPath:indexPath] accessoryType] == UITableViewCellAccessoryCheckmark)
{
//global array to store selected object
[checkMarkArray removeObject:[distanceArray objectAtIndex:indexPath.row]];
NSLog(@"array %@",checkMarkArray);
[tableViewDistance cellForRowAtIndexPath:indexPath].accessoryView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"unchecked.png"]] autorelease];
[[tableViewDistance cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryNone];
[self.tableViewDistance reloadData];
}
else
{
[checkMarkArray addObject:[distanceArray objectAtIndex:indexPath.row]];
NSLog(@"array again %@",checkMarkArray);
[tableViewDistance cellForRowAtIndexPath:indexPath].accessoryView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"checked.png"]] autorelease];
[[tableViewDistance cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];
[self.tableViewDistance reloadData];
}
tableViewDistance.delegate = self;
tableViewDistance.dataSource = self;
[self.tableViewDistance reloadData];
}
* viewDidLoad ** *
- (void)viewDidLoad
{
distanceArray= [[NSMutableArray alloc] initWithObjects:@"20 Kilomètres", @"40 Kilomètres", @"60 Kilomètres",@"80 Kilomètres",nil];
NSLog(@"distance %@",distanceArray);
tableViewDistance.backgroundColor=[UIColor clearColor];
checkMarkArray = [[NSMutableArray alloc] init];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
我在您的代碼中發現了一些弱點:
您應該將cell.accessoryView
設置為if (cell == nil){}
在- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
您可以使用tableView
而不使用tableViewDistance
(可能是nil
嗎?)
在- (void)viewDidLoad
您應該先調用[super viewDidLoad];
然后進行自定義。
希望能幫到你。 GL
將您的tableView出口(UITableView * tableViewDistance)連接到Xib TableView。
嘗試致電
[tableView reloadData];
代替
[self.tableViewDistance reloadData];
根據對問題的評論,您一次只能選擇一個單元格,因此您要取消選擇先前選擇的單元格,然后選擇當前選擇的單元格。 您可以使用UITableView
的-indexPathForSelectedRow
方法查找選定的行:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// For multiselect, use an array to store the checked state instead of using the tableView's state.
[self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow]];
[self.tableView cellForRowAtIndexPath:indexPath].accessoryView = ...;
}
您還將需要實現tableView:didDeselectRowAtIndexPath:
::
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.tableView cellForRowAtIndexPath:indexPath].accessoryView = ...;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.