Uitableview 动态修改单元格文本
我想动态修改我的单元格内容,即使在重新加载数据后,我的单元格也无法正常工作。正如预期的那样,当视图加载时,我在表上垂直获得1 1,当我按段1时,我获得4 4,正如预期的那样,然而,当我按2时,我再次获得-1-1-1-1-1,当我按3时,我再次获得4 4。此外,单元格的内容取决于我按下按钮的顺序。我的代码如下表中只有一行和多个部分:Uitableview 动态修改单元格文本,uitableview,cell,uisegmentedcontrol,Uitableview,Cell,Uisegmentedcontrol,我想动态修改我的单元格内容,即使在重新加载数据后,我的单元格也无法正常工作。正如预期的那样,当视图加载时,我在表上垂直获得1 1,当我按段1时,我获得4 4,正如预期的那样,然而,当我按2时,我再次获得-1-1-1-1-1,当我按3时,我再次获得4 4。此外,单元格的内容取决于我按下按钮的顺序。我的代码如下表中只有一行和多个部分: - (void)viewDidLoad { currentarray=[NSArray arrayWithObjects:@"-1",@"-2",@"-3", @
- (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";
}
}
}
-(void)viewDidLoad
{
currentarray=[NSArray arrayWithObjects:@“-1”、@“-2”、@“-3”、@“-4”、nil];
}
-(iAction)SegControl:(id)发送方{
对于(int i=0;i您需要将配置单元格的代码移到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];
}
}
-(iAction)SegControl:(id)发送方
{
开关(发送方。选择的分段索引){
案例0:
currentArray=sorteddectotalteamPoints;
打破
案例1:
currentArray=[NSArray arrayWithObjects:@“4”,“6”,“7”,“8”,nil];
打破
案例2:
currentArray=[NSArray arrayWithObjects:@“9”,“10”,“11”,“12”,nil];;
打破
案例3:
currentArray=[NSArray arrayWithObjects:@“13”,“14”,“15”,“16”,nil];
打破
}
//更新单元格
对于(int i=0;我知道您已经这样做了,但是您能进一步解释一下为什么我必须将它移出cell=nil语句。RegardAssure。当您重新加载一个单元格时,dequeueReusableCellWithIdentifier:
回收您正在重新加载的一个单元格并返回它。因此在到达之前,您的cell
变量已经设置好了e> 如果(cell==nil)
语句,则程序永远不会进入该块。它在第一次通过时工作正常,因为最初没有要回收的单元格,并且dequeueReusableCellWithIdentifier:
返回nil
,从而导致程序进入该块。