我正在使用我以前见过的一些代码。基本上用户在带有一些按钮的帖子上回答是或否。按是或否更新正常工作的数据库,它还会更新可见的UI,这不起作用。此UI更新按钮,以便选择一个按钮,突出显示其他按钮,并禁用这些按钮以进行用户交互。它还对两个UILabel进行了更改。这些按钮调用的方法需要更新数据库并从tableViewCell检索按钮并更新我在另一个ViewController中使用的方法的更改,所以我无法理解这里的区别。这是我的cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *simpleTableIdentifier = [NSString stringWithFormat:@"%ld,%ld",(long)indexPath.section,(long)indexPath.row];
NSLog(@" simple: %@",simpleTableIdentifier);
if (indexPath.row==0) {
ProfileFirstCell *cell = [self.tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
cell = [[ProfileFirstCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:simpleTableIdentifier];
}
cell = [self createProfileCell:cell];
return cell;
}else{
YesNoCell *cell =[self.tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell==nil) {
cell=[[YesNoCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell = [self createYesNoCell:cell:indexPath];
return cell;
}
}
基本上,它的作用是在第一个单元格中创建用户配置文件,并加载用户要求的所有问题。这是我在旧tableView和this tableView之间看到的主要区别。在createYesNoCell中,我创建了UIElements并创建了如下标记
cell.yesVoteButton.tag=indexPath.row+ yesVoteButtonTag1;
cell.noVoteButton.tag=indexPath.row+ noVoteButtonTag1;
cell.yesCountLabel.tag=indexPath.row+ yesCountLabelTag1;
cell.noCountLabel.tag=indexPath.row+ noCountLabelTag1;
按钮具有启动许多功能的选择器。它会找到以下按下的按钮。
NSInteger index;
if(sender.tag>=yesVoteButtonTag1){
NSLog(@"Yes button pressed");
votedYes=true;
index=sender.tag-yesVoteButtonTag1;
}else{
NSLog(@"No button Pressed");
votedYes=false;
index=sender.tag-noVoteButtonTag1;
}
UILabel *yesLabel = (UILabel*) [self.tableView viewWithTag:index+yesCountLabelTag1]; // you get your label reference here
UIButton *yesButton=(UIButton *)[self.tableView viewWithTag:index+1+yesVoteButtonTag1];
NSLog(@"Tag IN METHOD: %ld",index+yesVoteButtonTag1);
UILabel *noLabel = (UILabel*) [self.tableView viewWithTag:index+1+noCountLabelTag1]; // you get your label reference here
UIButton *noButton=(UIButton *)[self.tableView viewWithTag:index+noVoteButtonTag1];
当我查看它们时,这些viewWithTag调用是nil。我从之前的实现中看到的唯一区别是旧的有部分和一行,而这一行是所有行和一个部分。因此,用indexPath.row替换indexPath.section应该考虑到这一点。另外,我检查了在cellForRowAtIndexPath中生成的标记与在yes / no投票方法中恢复的行相同,因为由于在indexPath.row == 0处创建了配置文件单元格而将其替换为1。我尝试将单元格传递给是/否投票方法并尝试使用contentView恢复按钮和标签,作为对类似帖子的一些建议。然而,这似乎并没有解决我的问题。真的很感激对此的一些见解。
答案 0 :(得分:0)
你打电话给' [tableView reload]'更新UITableView的方法,可能会有所帮助。
答案 1 :(得分:0)
首先,表重用标识符应该用于单元格的类型,而不是每个单元格的一个。您有两种类型,因此您应该使用两个固定的重用标识符。
ProfileFirstCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"ProfileCell"];
if (cell == nil) {
cell = [[ProfileFirstCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:@"ProfileCell"];
}
和
YesNoCell *cell =[self.tableView dequeueReusableCellWithIdentifier:@"YesNoCell"];
if (cell==nil) {
cell=[[YesNoCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"YesNoCell"];
}
其次,在创建表格之后尝试获取对单元格的引用(而不是为您工作),您应该在创建单元格时完全初始化单元格。 (TableView不会创建单元格,除非它们可见,所以你根本不应该依赖它们的存在。)
createProfileCell
应该真正被称为initializeProfileCell
,因为您没有在其中创建单元格 - 您已经在上面的行中执行了此操作,或者恢复了旧单元格。
然后,您对initializeProfileCell
的调用可以带一个标志,指定它是Yes还是No单元格并相应地设置其属性。
cell = [self initializeProfileCell:cell isYes:(indexPath.section==0)];
与createYesNoCell
类似 - > initializeYesNoCell
。
答案 2 :(得分:0)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"YOURCELL_IDENTIFIER";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
UILabel *title = (UILabel*) [cell viewWithTag:5];
UILabel *vensu =(UILabel*) [cell viewWithTag:7];
vensu.text = @"YOUR TEXT";
title.text = @"YOUR TEXT";
return cell;
}