Я не уверен, что это правильный способ создания UITableViewCell в коде, но он работает отлично, пока мне не нужно перезагрузить таблицу, так как она возвращает старые ячейки, не загружая новый контент.
- (UITableViewCell *)tableView:(UITableView *)tableViewData cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableViewData dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
if (indexPath.section == 0) {
if ([marker objectForKey:@"imageUrl"]) {
UIView *transparentBackground = [[UIView alloc] initWithFrame:CGRectZero];
transparentBackground.backgroundColor = [UIColor clearColor];
cell.backgroundView = transparentBackground;
UIImageView *buildingView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 10, 100, 100)];
buildingView.image = [self imageWithImage:[ImageManipulator makeRoundCornerImage:[self loadImage:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.qut.edu.au%@", [marker objectForKey:@"imageUrl"]]]] :9 :9]];
[cell addSubview:buildingView];
} else {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 200, 15)];
label.text = @"test";
[cell addSubview:label];
[label release];
}
} else {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 200, 15)];
label.text = @"test";
[cell addSubview:label];
[label release];
}
}
return cell;
}
По сути, ячейка, возвращаемая в строке 3, не равна nil (это старая), поэтому оператор if не запускается и возвращает старую ячейку.Я мог бы загрузить новую ячейку каждый раз, но это будет иметь проблемы с загрузкой и использованием памяти.
Какой правильный способ сделать это?