分段 UITableView 中的按钮标记问题
Posted
技术标签:
【中文标题】分段 UITableView 中的按钮标记问题【英文标题】:Issue with button tag in sectioned UITableView 【发布时间】:2011-10-28 12:13:38 【问题描述】:我在分段的 tableview 单元格中有两个按钮,竖起大拇指和竖起大拇指。 最初没有选择两个按钮的图像。当我选择竖起大拇指按钮时,它的图像变为竖起大拇指选中,而另一个变为竖起大拇指未选中,反之亦然。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
enter code hereNSLog(@"mod:numberOfSectionsInTableView");
NSLog(@"[preferences count]=%d",[preferences count]);
return [preferences count];
-(NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
enter code here
enter code herereturn [self.choices count];
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexpath
NSLog(@"cellForRowAtIndexPath: DISPLAY TEST");
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
NSLog(@"Text is: %@", [choices objectAtIndex:indexpath.row]);
NSLog(@"CHOICE AT INDEX PATH IS: %@",[choices objectAtIndex:indexpath.row %[choices count]]);
cell.textColor = [UIColor whiteColor];
cell.backgroundColor = [UIColor blackColor];
// Thumbs up button.
//UIButton *thumbsUp = [[UIButton alloc]init];
thumbsUp = [UIButton buttonWithType:UIButtonTypeCustom];
[thumbsUp setTag:(indexpath.row+(indexpath.section * 50))];
[thumbsUp addTarget:self
action:@selector(thumbUp_ButtonClicked:event:)
forControlEvents:UIControlEventTouchUpInside];
[thumbsUp setTitle:@"" forState:UIControlStateNormal];
thumbsUp.frame = CGRectMake(150.0, 20, 20, 15);
[thumbsUp setBackgroundImage:[UIImage imageNamed:@"thumbsup_not_selected.png"]
forState:UIControlStateNormal];
//NSLog(@"------------------>TAG TEST : %d",(indexpath.row+(indexpath.section * 50)));
[cell.contentView addSubview:thumbsUp];
// Thumbs down button
thumbsDown = [UIButton buttonWithType:UIButtonTypeCustom];
[thumbsDown addTarget:self
action:@selector(thumbDown_ButtonClicked:event:)
forControlEvents:UIControlEventTouchUpInside];
[thumbsDown setTitle:@"" forState:UIControlStateNormal];
thumbsDown.frame = CGRectMake(200, 20, 20, 15);
[thumbsDown setTag:indexpath.row+120];
[cell.contentView addSubview:thumbsDown];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[thumbsDown setBackgroundImage:[UIImage imageNamed:@"thumbsdown_not_selected.png"]forState:UIControlStateNormal];
NSLog(@"------------> TAG TEST %d",thumbsUp.tag);
cell.text = [choices objectAtIndex:(indexpath.row % [choices count])];
NSLog(@"HELLO FOR TESTING");
return cell;
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
if (sectionTitle == nil)
return nil;
// Create label with section title
UILabel *label = [[[UILabel alloc] init] autorelease];
label.frame = CGRectMake(15, 10, 300, 25);
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor blackColor];
label.shadowColor = [UIColor whiteColor];
label.shadowOffset = CGSizeMake(0.0, 1.0);
label.font = [UIFont boldSystemFontOfSize:16];
label.textAlignment = UITextAlignmentLeft;
label.text = sectionTitle;
// Create header view and add label as a subview
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(12, 0, 300, 60)];
[view autorelease];
[view addSubview:label];
//[view addSubview:segmentedControl];
view.backgroundColor = [UIColor grayColor];
return view;
//Thumbs Up Button Action
- (IBAction)thumbUp_ButtonClicked:(id)sender event:(id)event
NSLog(@"Thumbs Up Check!");
UIButton *button = (UIButton *)sender;
UITableViewCell *cell = (UITableViewCell *) [[button superview] superview];
NSIndexPath *indexPath = [myTable indexPathForCell:cell];
NSLog(@"indexpath =%d",indexPath.row);
//[button setTag:indexPath.row+(indexPath.section * 50)];
int cTag=[sender tag];
NSLog(@"------>TAG : %d", cTag);
NSLog(@"------> Calculated TAG %d",indexPath.row+(indexPath.section * 50));
if(cTag == (indexPath.row+(indexPath.section * 50)))
NSLog(@"BUTTON COUNT:");
[button setBackgroundImage:[UIImage imageNamed:@"thumbsup_selected.png"]
forState:UIControlStateNormal];
NSInteger section = indexPath.section;
NSInteger row = indexPath.row;
//int row = button.tag;
NSLog(@"SECTION IS:%d",section);
NSLog(@"ROW IS: %d",row);
NSArray *array = cell.contentView.subviews;
NSLog(@"NUMBER OF OBJECTS: %d",[array count]);
UIButton *test = (UIButton *)[array objectAtIndex:2];
[test setBackgroundImage:[UIImage imageNamed:@"thumbsdown_not_selected.png"]forState:UIControlStateNormal];
由于在我更改一个按钮的图像时按钮标签出现问题,因此多个按钮正在更改。如果有人可以请找到解决方案,这将有所帮助....
【问题讨论】:
【参考方案1】:由于表格视图单元被回收,这是很常见的情况。您的带有按钮标签的方案相当复杂且容易出错。
在表格视图的数据源数据模型中跟踪每个单元格的 ThumbsUp/Down 状态会更有意义。然后,在cellForRowAtIndexPath:
中明确设置每个按钮的状态。
顺便说一句:cell.text
和 cell.textColor
已弃用。您应该使用cell.textLabel
的属性。
【讨论】:
如果有人可以请提供代码来纠正问题,这将有所帮助。问题在于为按钮设置标签...以上是关于分段 UITableView 中的按钮标记问题的主要内容,如果未能解决你的问题,请参考以下文章