how to clear uiableviewcell
Hi I have a single section of custom uitableview cells, which I am assigning values from a subview when they are chosen by the user.
The custom cell is made up of two textlabels, one a title the other is where the chosen element from the subview will be assigned (while its not assingned it has the string "empty" assigned to it.
I would like to have the option to swipe a single cell and then enable the user to clear the contents of the uilabel back to "empty" if the user has assigned a value to it.
I have tried to do this, and have got it working to a point, however I am having issues with the uitableview becoming unresponsive untill the user touches the tableview again.. then it works like normal.
This is my implementation, I would like to know if this is the only way or if their is a better way of doing this, as I am having issues with responsiveness of the table once I have used a swipe.
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
return @"Clear";
}
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
//Clears the correct uitableviewcell and releases the correct nsstring in the view
if (indexPath.section == 0) {
if (indexPath.row == 0) {
//reset uitableviewcell textlabel开发者_如何学运维 with default input "empty"
vehicleSearchCell = [tableView cellForRowAtIndexPath:indexPath];
UILabel *label2;
label2 = (UILabel *)[vehicleSearchCell viewWithTag:2];
label2.text = @"empty";
[tableView reloadData]; //may or may not need to call this now
}
else if (indexPath.row == 1) {
//reset uitableviewcell textlabel with default input "empty"
}
else if (indexPath.row == 2) {
//reset uitableviewcell textlabel with default input "empty"
}
else (indexPath.row == 3) {
//reset uitableviewcell textlabel with default input "empty"
}
}
}
}
Future update. I also have another custom cell I am using for a button (the custom cell has a button in it) and the user is also able to swipe this (which i dont want however) the clear button also shows on this but after the clear button has been used (which obviously dose nothing) you can press another cell first time, there is no delay.. but if you are to do it with any of the cells in section 0 then you get the unresponsiveness..
Update I am starting to think its the way I am loading my cells using tags etc.. do you guys thing this might be causing the problem? if so how can I solve this..
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:@"VehicleSearchCell" owner:self options:nil];
cell = vehicleSearchCell;
self.vehicleSearchCell = nil;
}
// Configure the cell...
cell.selectionStyle = UITableViewCellSelectionStyleNone;
if(indexPath.section == 0)
{
if(indexPath.row == 0)
{
UILabel *label1;
label1 = (UILabel *)[cell viewWithTag:1];
label1.text = @"Manufacture";
UILabel *label2;
label2 = (UILabel *)[cell viewWithTag:2];
label2.text = manufactureSearchObjectString;
}
else if(indexPath.row == 1)
{
[[NSBundle mainBundle] loadNibNamed:@"VehicleSearchCellDual" owner:self options:nil];
cell = vehicleSearchCellDual;
self.vehicleSearchCellDual = nil;
UILabel *label;
label = (UILabel *)[cell viewWithTag:1];
label.text = @"Model";
UILabel *label2;
label2 = (UILabel *)[cell viewWithTag:2];
label2.text = @"empty";
UILabel *label3;
label3 = (UILabel *)[cell viewWithTag:3];
label3.text = @"empty";
}
else if(indexPath.row == 2)
{
UILabel *label;
label = (UILabel *)[cell viewWithTag:1];
label.text = @"Year";
UILabel *label2;
label2 = (UILabel *)[cell viewWithTag:2];
label2.text = @"empty";
}
else if(indexPath.row == 3)
{
UILabel *label;
label = (UILabel *)[cell viewWithTag:1];
label.text = @"Key type";
UILabel *label2;
label2 = (UILabel *)[cell viewWithTag:2];
label2.text = keyTypeSearchObjectString;
}
}
else if(indexPath.section == 1)
{
if(indexPath.row == 0)
{
[[NSBundle mainBundle] loadNibNamed:@"VehicleSearchButtonCell" owner:self options:nil];
cell = searchButtonCell;
self.searchButtonCell = nil;
}
}
return cell;
}
I'm slightly confused and worried by something in your code
manufactureSearchObjectString = @"empty"; //Is manufactureSearchObjectString a member variable?
[self.tableView reloadData]; //reloads the tabels so you can see the value.
[manufactureSearchObjectString release]; //Why do you release this here?? This will leak/throw a wobbly more than likely.
if manufactureSearchObjectString is a member variable for your class I assume that you have allocated it at some point. You would want to release it first before pointing it somewhere else. So the above code becomes
[manufactureSearchObjectString release];
manufactureSearchObjectString = [[NSString alloc] initWithString:@"empty"];
[self.tableView reloadData]; //reloads the tabels so you can see the value.
If you have not allocated it anywhere else before then there is no need for the release at all.
Also you may want to look into using a switch statement, or even using "else if's" instead of plain if statements as
if (indexPath.row == 0) //if row is 0 test is correct
{
}
if (indexPath.row == 1) //if row is 0 and passed the first test this still gets tested here
{
}
if (indexPath.row == 2) //same again row gets tested again when it's already passed the == 0 test
Change this to
if (indexPath.row == 0) //if row is 0 test is correct
{
}
else if (indexPath.row == 1) //if row is 0 it's not so will not get tested again
{
}
else if (indexPath.row == 2)
{}
else //remember to finish with a plain else
The second option will run faster than the first for most cases as the == test will only occur until a success is reached. The length of time this takes would depend upon the number of rows and sections you have in your table of course.
However if you want to have a more generic approach to this you could call cellForRowAtIndexPath: and access the string there directly.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
CustomUITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
[cell setSecondCustomTextField:@"empty"]; //or could be cell.secondCustomTextField = @empty";
[tableView reloadData]; //may or may not need to call this now
}
}
I've assumed that you've got a setter for whatever you've called your editable textfield in your custom tableview cell. If not.. you should make one.
EDIT:
Alright you're calling
[[NSBundle mainBundle] loadNibNamed:@"VehicleSearchCellDual" owner:self options:nil];
but not referencing it to anything, you don't need to do this. I take it you are trying to load your custom UITableViewCell from it's NIB, but you're going about this the wrong way. I suggest you have a read of this Coco with Love post regarding Custom Table view cells.
I feel that for the addition of two text fields to a UITableViewCell that a custom NIB is slightly overkill however as you've chosen to do it that way I'll answer your question that way.
For a start, there isn't much point having three custom cells when each of them are so similar. You Can just create one custom UITableViewCell Nib file (this will also make your code easier to read), so we'll use your nib file for "VehicleSearchCellDual" as this has the majority of the features that we're after, ignore the other two "VehicleSearchCell" and "VehicleSearchButtonCell" as you wont be needing them. Now copy the UIButton out of "VehicleSearchButtonCell" and insert it into the "VehicleSearchCellDual" and set it to hidden in interface builder, lets also give it the tag value of 4. As long as you've set the tags in interface builder to match the values you're calling below then calling viewWithTag: should return what you are after, now seeing as we're going to use your "VehicleSearchCellDual" Nib I'll be assuming that Tag 1 corresponds to the left most text field, tag 2 is the central text field, and tag 3 is the right most text field.
Ok now you've done that you can load your table cells in the following manner.
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"VehicleSearchCellDual" owner:self options:nil];
for( NSObject* nibItem in nib )
{
if( [nibItem isKindOfClass:[UITableViewCell class]] )
{
cell = (UITableViewCell*)nibItem;
break;
}
}
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
if(indexPath.section == 0)
{
UILabel* titleLabel;
titleLabel = (UILabel *)[cell viewWithTag:1];
UILabel* rightMostLabel;
rightMostLabel = (UILabel *)[cell viewWithTag:3];
if(indexPath.row == 0)
{
[titleLabel setText:@"Manufacture"];
[rightMostLabel setText:manufactureSearchObjectString];
}
else if(indexPath.row == 1)
{
[titleLabel setText:@"Model"];
[rightMostLabel setText:@"empty"];
UILabel* leftMostLabel;
leftMostLabel = (UILabel *)[cell viewWithTag:2];
leftMostLabel.text = @"empty";
}
else if(indexPath.row == 2)
{
[titleLabel setText:@"Year"];
[rightMostLabel setText:@"empty"];
}
else if(indexPath.row == 3)
{
[titleLabel setText:@"Key type"];
[rightMostLabel setText:keyTypeSearchObjectString];
}
}
else if(indexPath.section == 1)
{
if(indexPath.row == 0)
{
[[cell viewWithTag:1] setHidden:YES];
[[cell viewWithTag:2] setHidden:YES];
[[cell viewWithTag:3] setHidden:YES];
[[cell viewWithTag:4] setHidden:NO];
}
}
return cell;
Now above where you're setting rightMostLabel and leftMostLabel to @"empty" you may want to set them to whatever member variable 'searchString' is appropriate for that field. Otherwise each time the table gets reloaded, those field values will get set to "empty". That should set you on the right track.
So I have solved the problem!... I basically deleted the views .h.m.xib started again pretty much copied my code right across to the file and it works perfectly... I have no idea why this happened maybe something I did when I very first set it up.. I'm not sure...
精彩评论