Custom UIButton in UITableViewCell - Remove does not work
I got a custom UIButton in my TableViewCell via:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *ident = @"indet";
cell = [tableView dequeueReusableCellWithIdentifier:ident];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:ident] autorelease];
}
button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame: CGRectMake( 230.0f, 7.5f, 43.0f, 43.0f)];
[button setImage:[UIImage imageNamed:@"check_bak.png"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(removeEntry) forControlEvents:UIControlEventTouchUpInside];
button.tag = [indexPath row];
[cell addSubview:button];
cell.textLabel.text = [myArray objectAtIndex:indexPath.row];
cell.textLabel.textColor = [UIColor blackColor];
cel开发者_JAVA百科l.textLabel.font = [UIFont fontWithName:@"Helvetica" size:20.0];
cell.textLabel.shadowColor = [UIColor whiteColor];
cell.textLabel.shadowOffset = CGSizeMake(0,1);
return cell;
}
I declared the indexPath in my .h for the removeEntry function.
removeEntry:
[myArray removeObjectAtIndex:indexPath.row];
[myTable reloadData];
myArray is a NSMutableArray.
This way does not work properly.
Everytime I remove an entry at indexPath.row it does remove an entry. But the wrong. It's always one entry above which is getting removed. Even if I do indexPath.row+1 / -1.
Is there another way? The Button should stay in the cell.
I hope it's understandable, sorry, I'm german. :)
How many sections are in your table view? It may be a good idea to print out to the console the value of indexPath.row in your removeEntry method. If you click on the first row, is 0 being printed out? Fifth row, is 4 being printed out, etc.
EDIT: Looking at your code, you're storing the indexpath as the tag of the button. In this case, change your removeEntry method to look like this (you can change you return type to whatever you want to return):
- (void)removeEntry:(id)sender {
And add a colon after 'removeEntry' when you're adding your target:
[button addTarget:self action:@selector(removeEntry:) forControlEvents:UIControlEventTouchUpInside];
Now, inside removeEntry, you can do:
UIButton *button = (UIButton *)sender;
[myArray removeObjectAtIndex:button.tag];
[myTable reloadData];
Use the following peace of code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *ident = @"indet";
cell = [tableView dequeueReusableCellWithIdentifier:ident];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:ident] autorelease];
}
if ([cell.contentView subviews]){
for (UIView *subview in [cell.contentView subviews]) {
[subview removeFromSuperview];
}
}
//below here your piece of code.
}
The reason for this is, in the method we reuse the cell, that preserves all the subviews added to the cell and only refreshes the text part of the cell.
Hope this will work for you!!
I have encountered the same problem in my iOS coding with UITableView. I have fixed it by adding button on cell.contentView and removing it from cell.contentView.
Add button with
[cell.contentView addSubview:button];
instead of [cell addSubview:button];
And add following code lines into cellForRowIndexPath() method before add views to cell to remove button from cell.
for(UIView *subview in [cell.contentView subviews]){
[subview removeFromSuperView];
}
It would work correctly.
精彩评论