tableView reloadData无法正常工作:不调用cellForRowAtIndexPath [英] tableView reloadData not working: cellForRowAtIndexPath not called
问题描述
我有此问题,reloadingData
未调用cellForRowAtIndexPath
.我已经花了很多时间了.我的xib文件dataSource
和delegate
连接到File's Owner
.任何想法都欢迎.
I have this problem cellForRowAtIndexPath
is not called by reloadingData
. I have spent a lot of time with this. I xib file dataSource
and delegate
are connected to File's Owner
. Any idea is welcome.
这是我的代码:
* cellForRowAtIndexPath *
*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 * ***
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 ** *
*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.
}
推荐答案
我在您的代码中发现了一些薄弱环节:
I've found some weak parts in your code:
-
您应将
cell.accessoryView
设置为if (cell == nil){}
在- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
中,您可以使用tableView
而不是tableViewDistance
(可能是nil
?)
In - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
you can use tableView
and not use tableViewDistance
(may be it is nil
?)
在- (void)viewDidLoad
中,您应该先调用[super viewDidLoad];
,然后进行自定义.
In - (void)viewDidLoad
you should call first [super viewDidLoad];
and then make customizations.
希望对您有所帮助. GL
Hope that will help you. Gl
这篇关于tableView reloadData无法正常工作:不调用cellForRowAtIndexPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!