reloaddata없이 tableview의 레이블 텍스트를 업데이트하고 싶습니다. 나는 시도 ((uilabel *)[self.tableview viewWtihTag:10]).text = Newstring
하지만 작동하지 않습니다.
맞습니까? 아니면 다른 해결책이 있습니까?
이 방법을 사용하는 - (void)reloadRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
것이 정확합니다. 그러나 각 셀의 모든 행에로드 할 값을 추적 한 다음 사용자가 테이블을 스크롤 할 때 행이 다시로드 될 때마다 액세스해야합니다.
다음은 샘플입니다.
@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>
@property NSMutableArray *labelValueListForSection0;
@property NSMutableArray *labelValueListForSection1;
@property NSMutableArray *labelValueListForSection2;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
_labelValueListForSection2 = [[NSMutableArray alloc] initWithObjects:@"value1", @"value2", @"value3", nil];
}
- (void)changeAnItem
{
[_labelValueListForSection2 setObject:@"ChangedValue" atIndexedSubscript:1];
[_table reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:1 inSection:2]] withRowAnimation:UITableViewRowAnimationNone];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"SimpleTableItem";
UITableViewCell *tableCell = [tableView dequeueReusableCellWithIdentifier:identifier];
if(tableCell == nil)
{
tableCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
switch(indexPath.section)
{
case 0:
tableCell.textLabel.text = @"Test";
break;
case 1:
tableCell.textLabel.text = @"Test";
break;
case 2:
tableCell.textLabel.text = [_labelValueListForSection2 objectAtIndex:indexPath.row];
break;
case 3:
tableCell.textLabel.text = @"Test";
break;
case 4:
tableCell.textLabel.text = @"Test";
break;
default:
break;
}
return tableCell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 5;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 3;
}
@end
보시다시피 각 행의 값을 추적하고 배열 안에 저장하고 있습니다. 제 경우에는 특정 섹션의 각 행에 대한 값 배열이 있습니다. 그래서이 방법은
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
호출되면 해당 행에 할당되어야하는 값을 가져옵니다.
모르는 경우 iOS는 더 나은 성능과 더 나은 메모리 관리를 위해 각 테이블 셀을 재사용하고 있습니다. 따라서 내 행 중 하나의 값이 다른 행에서 중복되는 이유와 같은 경우가 발생하면 tableCell의 해당 인스턴스가 다른 행에서 재사용되기 때문입니다.
따라서 셀이로드 될 때마다 값이 정확해야합니다. 각 행의 값을 추적하고 다시로드 될 때마다 해당 셀에 다시 할당해야합니다.
이것이 문제를 해결하는 데 도움이되기를 바랍니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다