#ios #objective-c #uitableview
#iOS #objective-c #uitableview
Вопрос:
Я пытаюсь создать пользовательскую ячейку таблицы, uitableview имеет 4 строки, но создает 2 ячейки.
Я добавляю представления динамически в метод cellForRowAtIndexPath. Он добавляет первую строку и последнюю строку.
Что я делаю не так?
Мой код выглядит следующим образом
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// NSLog(@"cellForRowAtIndexPath");
//creating table rows
static NSString *CellIdentifier = @"TableCell";
STGTableViewCell *cell = (STGTableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//NSMutableDictionary * temp = [((STGTableView*)tableView).tableDataSource objectAtIndex:[indexPath row]];
if (cell == nil) {
//cell.globalView = ((STGTableView*)tableView).cellView;
cell = [[STGTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
//[[STGTableViewCell alloc] initWithWiew:((STGTableView*)tableView).cellView];
//cell = ((STGTableView*)tableView).globalCell;
NSLog(@"((STGTableView*)tableView).globalCell rowno:%dn",[indexPath row]);
UIView * tempCell = ((STGTableView*)tableView).cellView;
for(UIView *sview in [tempCell subviews])
{
if([sview isKindOfClass:[UIScrollView class]])
{
[cell.contentView addSubview:sview];
}
}
NSLog(@"inside alloc");
}
NSLog(@"outside alloc");
for(UIView *sview in [cell.contentView subviews])
{
if([sview isKindOfClass:[UIScrollView class]])
{
for(UIView *subview in [sview subviews])
{
if([subview isKindOfClass:[UILabel class]])
{
NSMutableDictionary *tempDict = [((STGTableView*)tableView).tableDataSource objectAtIndex:[indexPath row]];
NSLog(@"%d",[indexPath row]);
NSLog(@"%@",((UILabel*)subview).text);
if([((UILabel*)subview).text isKindOfClass:[NSString class]] amp;amp;
![((UILabel*)subview).text isKindOfClass:[NSNumber class]])
{
NSRange range = [((UILabel*)subview).text rangeOfString:@"#"];
if (range.location != NSNotFound)
{
if([[((UILabel*)subview).text substringToIndex:1] isEqualToString:@"#"])
{
NSLog(@"%@",((UILabel*)subview).text);
NSString *tempKey = [((UILabel*)subview).text stringByReplacingOccurrencesOfString:@"#" withString:@""];
[((UILabel*)subview) setText:[NSString stringWithFormat:@"%@",[tempDict objectForKey:tempKey]]];
}
}
}
}
else
{
}
}
}
}
return cell;
}
Ответ №1:
Вы впервые добавили scrollview в представление содержимого. Как если бы (cell == nil) условие, которое вызывалось один раз изначально. Измените свой код, как показано ниже…
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// NSLog(@"cellForRowAtIndexPath");
//creating table rows
static NSString *CellIdentifier = @"TableCell";
STGTableViewCell *cell = (STGTableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//NSMutableDictionary * temp = [((STGTableView*)tableView).tableDataSource objectAtIndex:[indexPath row]];
if (cell == nil)
{
//cell.globalView = ((STGTableView*)tableView).cellView;
cell = [[STGTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
//[[STGTableViewCell alloc] initWithWiew:((STGTableView*)tableView).cellView];
//cell = ((STGTableView*)tableView).globalCell;
NSLog(@"((STGTableView*)tableView).globalCell rowno:%dn",[indexPath row]);
NSLog(@"inside alloc");
}
NSLog(@"outside alloc");
UIView * tempCell = ((STGTableView*)tableView).cellView;
for(UIView *sview in [tempCell subviews])
{
if([sview isKindOfClass:[UIScrollView class]])
{
[cell.contentView addSubview:sview];
}
}
for(UIView *sview in [cell.contentView subviews])
{
if([sview isKindOfClass:[UIScrollView class]])
{
for(UIView *subview in [sview subviews])
{
if([subview isKindOfClass:[UILabel class]])
{
NSMutableDictionary *tempDict = [((STGTableView*)tableView).tableDataSource objectAtIndex:[indexPath row]];
NSLog(@"%d",[indexPath row]);
NSLog(@"%@",((UILabel*)subview).text);
if([((UILabel*)subview).text isKindOfClass:[NSString class]] amp;amp;
![((UILabel*)subview).text isKindOfClass:[NSNumber class]])
{
NSRange range = [((UILabel*)subview).text rangeOfString:@"#"];
if (range.location != NSNotFound)
{
if([[((UILabel*)subview).text substringToIndex:1] isEqualToString:@"#"])
{
NSLog(@"%@",((UILabel*)subview).text);
NSString *tempKey = [((UILabel*)subview).text stringByReplacingOccurrencesOfString:@"#" withString:@""];
[((UILabel*)subview) setText:[NSString stringWithFormat:@"%@",[tempDict objectForKey:tempKey]]];
}
}
}
}
else
{
}
}
}
}
return cell;
}
Надеюсь, это поможет вам ..!
Комментарии:
1. к сожалению, это мне не помогло, результат тот же, записаны первая и последняя строка
2. вы устанавливаете значение для своей метки только тогда, когда оно соответствует диапазону.. Таким образом, количество строк возвращает четыре. но диапазон соответствует только первой и последней строке.. Проверьте, если условие if (диапазон. местоположение != NSNotFound) не удается .. установите некоторое значение в UILabel..
3. я написал NSLog внутри метода cellForRowAtIndexPath, он регистрируется дважды, но количество исходных массивов табличных данных равно 4
4. Вам нужно получить значения UILabel, которые соответствуют диапазону в NSMutableArray в viewDidLoad или любым другим методом.. Затем вызовите метод reloadData, прежде чем устанавливать это количество массивов для numberofrows .. также отображать текст из этого массива…