我想动态修改我单元格的内容,即使重新加载数据后我也无法正常工作。正如预期的那样,当视图加载时,我在表格上垂直得到1 1 1 1 1 1 1,当我按下段1时,我按预期得到4 4 4 4 4 4 4,然而,当我按下2时,我得到-1 - 再次1 -1 -1 -1 -1,当我按3时,再次得到4 4 4 4 4 4 4。此外,我的细胞内容取决于我按下按钮的顺序。我的代码在下面的表中只有一行和多个部分:
- (void)viewDidLoad
{
currentarray=[NSArray arrayWithObjects:@"-1",@"-2",@"-3", @"-4", nil];
}
- (IBAction)SegControl: (id) sender{
for(int i=0; i<4; i++){
if(theSegControl.selectedSegmentIndex==i){
buttonpressed[i]=[NSNumber numberWithInt:1];
}
else {
buttonpressed[i]=[NSNumber numberWithInt:0];
}
}
if([buttonpressed[0] intValue]==1){
currentarray=sorteddectotalteamPoints;
}
else if([buttonpressed[1] intValue]==1){
currentarray=[NSArray arrayWithObjects:@"4",@"6",@"7", @"8", nil]; NSLog(@"%@",@"two pressed");
}
else if([buttonpressed[2] intValue]==1){
currentarray=[NSArray arrayWithObjects:@"9",@"10",@"11", @"12", nil]; NSLog(@"%@",@"three pressed");
}
else {
currentarray=[NSArray arrayWithObjects:@"13",@"14",@"15", @"16", nil]; NSLog(@"%@",@"four pressed");
}
//to update the cells
for(int i=0; i<[teamnameswithoutRepeat count];i++){
[byTeam reloadSections:[NSIndexSet indexSetWithIndex:i] withRowAnimation:UITableViewRowAnimationFade];
}
NSLog(@"%@",currentarray);
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, tableView.rowHeight)] ;
label.font = [UIFont fontWithName:@"Helvetica-Bold" size:14];
label.layer.borderColor = [UIColor whiteColor].CGColor;
label.layer.borderWidth = 1.0;
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor blackColor];
NSString *MyIdentifier = [NSString stringWithFormat:@"MyIdentifier %i", indexPath.section];
MyTableCell *cell = (MyTableCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[MyTableCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:MyIdentifier] ;
if(tableView==byTeam)
{
//[tableView reloadData];
label.text=[currentarray objectAtIndex:0];
[cell.contentView addSubview:label];
label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleHeight;
// UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]];
//cell.textLabel.text = @"updated text";
}
}
}
答案 0 :(得分:0)
您需要移动在if (cell == nil)
块之外配置单元格的代码,以便在重新加载单元格时执行它们,然后通过dequeueReusableCellWithIdentifier:
回收它们:
if (cell == nil) {
cell = [[MyTableCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:MyIdentifier] ;
}
if(tableView==byTeam) {
label.text=[currentarray objectAtIndex:0];
//...
}
此外,除非我遗漏了某些内容,否则可以通过消除buttonPressed
变量来简化分段控制处理程序。像这样的东西,例如:
- (IBAction)SegControl:(id)sender
{
switch(sender.selectedSegmentIndex) {
case 0:
currentArray = sorteddectotalteamPoints;
break;
case 1:
currentArray = [NSArray arrayWithObjects:@"4",@"6",@"7", @"8", nil];
break;
case 2:
currentArray = [NSArray arrayWithObjects:@"9",@"10",@"11", @"12", nil];;
break;
case 3:
currentArray = [NSArray arrayWithObjects:@"13",@"14",@"15", @"16", nil];
break;
}
//to update the cells
for(int i=0; i<[teamnameswithoutRepeat count];i++) {
[byTeam reloadSections:[NSIndexSet indexSetWithIndex:i] withRowAnimation:UITableViewRowAnimationFade];
}
}