UITableViewCell cell not showing up
Is there any met开发者_开发百科hod in the UITableView
for the tableviewcell when you are sliding the tableview and cells are being hidden or deleted. I have this code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
int curIndex = 0;
for (int i = 0; i < [dataHolder.dateArray count]; i++)
{
if ([[dataHolder.dateArray objectAtIndex:i] isEqual:[dataHolder.allDates objectAtIndex:[indexPath section]]])
{
if ([self indexHasContains:i] == NO)
{
curIndex = i;
[indexHasChossen addObject:[NSString stringWithFormat:@"%i", i]];
break;
}
}
}
cell.selectionStyle = UITableViewCellSelectionStyleGray;
cell.textLabel.text = [NSString stringWithFormat:@"%@, %@", [dataHolder.courseArray objectAtIndex:curIndex], [dataHolder.placeArray objectAtIndex:curIndex]];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.detailTextLabel.text = [NSString stringWithFormat:@"%@", [dataHolder.timeArray objectAtIndex:curIndex]];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
cell.textLabel.textColor = [UIColor blackColor];
cell.detailTextLabel.textColor = [UIColor whiteColor];
return cell;
}
I also want a method that delets from the indexHasChossen array when a cell is being hidden/deleted. I have looked through the apple dokumentation and haven’t find anything yet. Do any one know any way to do this?
It doesn't matter how tableView:cellForRowAtIndexPath:
if you want to hide or delete cells. The table view calls this method only when it knows cells exist. It depends on what you return in the methods numberOfSectionsInTableView:
and tableView:numberOfRowsInSection:
method. Most of the times the former returns 1
so if you want to eliminate an entire section than you should've some kind of marker such as sectionHidden
which is boolean value indicating whether section is hidden or not.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
if ( sectionHidden )
return 0;
else
return 1;
}
and wherever you want to initiate the delete action do something like this,
sectionHidden = YES;
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0]
withRowAnimation:UITableViewRowAnimationFade];
and to flip it back on do sectionHidden = NO
and call reloadSections:withRowAnimation:
.
Same thing applies for rows, where you will have to alter the tableView:numberOfRowsInSection:
method to reflect that you've deleted the rows or hidden the rows. This time you've to use reloadRowsAtIndexPaths:withRowAnimation:
instead of reloadSections:withRowAnimation:
method.
Although there's no standard concept of "hidden" tableViewCells, cells deleted by the user are reported in tableView:commitEditingStyle:forRowAtIndexPath:
But let me also add that you seem to be tracking "hasChosen" in cellForRowAtIndexPath . This method only means that the cell is about to appear on screen, not that it's been chosen. "Chosen" occurs when your delegate is called with tableView:didSelectRowAtIndexPath:
Edit: ah, maybe by "hidden", you mean that it's gone off-screen. No, I don't believe there is such a call, (although you could cheat a bit and look at any dequeued cells you get, as those are cells that were formerly on-screen and are now available).
Deleting cells from uitableview is easy. I recommend taking a look on the iPhoneCoreDataRecipes project from the apple developer docs.
You will have to add a function called commitEditingStyle to your UITableViewDelegate, and add the edit button (self.editButtonItem in UITableViewController) to allow editing mode.
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the object for the given index path
}
}
精彩评论