I am mak开发者_如何学JAVAing a UITableView
that allows cells to be selected (like the languanges `TableView\, but once one) but the checkmark accessory isn't working.
if (tableView == self.numberTableView) {
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
cell.text = [arryNumber objectAtIndex:indexPath.row];
if ([cell.textLabel.text isEqualToString:@"0"])
{
if (inumberfont == 0) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
if ([cell.textLabel.text isEqualToString:@"1"])
{
if (inumberfont == 1) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
if ([cell.textLabel.text isEqualToString:@"2"])
{
if (inumberfont == 2) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
if ([cell.textLabel.text isEqualToString:@"3"])
{
if (inumberfont == 3) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
if ([cell.textLabel.text isEqualToString:@"4"])
{
if (inumberfont == 4) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
}
Can someone help?
I rewrote your code.
if (tableView == self.numberTableView) {
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
cell.text = [NSString stringWithFormat:@"%i", indexPath.row];
if (inumberfont == indexPath.row) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
The above code does the exact same thing as your code, but well, your code wasn't working. In the following code I've fixed the errors I spotted. I also added some missing code. If you had it before, but didn't include it, that's fine; I just wanted to make sure that it wasn't missing.
if (tableView == self.numberTableView) {
static NSString *CellIdentifier = @"NumberCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [NSString stringWithFormat:@"%i", indexPath.row];
if (inumberfont == indexPath.row) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
I'm not sure what you mean by "the checkmark accessory isn't work", but try improving your code with my suggestions and if it still doesn't work, drop a comment and I'll see what I can do to help you further.
EDIT: Found out what the problem was. Try the following:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
inumberfont = indexPath.row;
[tableView reloadData];
}
What is the line:
cell.text = [arryNumber objectAtIndex:indexPath.row];
What is cell.text
? Don't you mean cell.textLabel.text
By the way I think you could probably condense your code by doing:
if ([cell.textLabel.text intValue] == inumberfont) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
You wouldn't need all 8 if statements just that one.
精彩评论