2016-02-23 4 views
1

Я лучше в ios, может ли кто-нибудь сказать мне, что я управляю несколькими изменяемыми массивами? например: у меня есть 3 массива в том, что один массив для изображения, а два других массива содержат имя и описание, и я хочу отображать на ячейке UITableViewМножественный Mutable Array в IOS

Я попробовал это, но он разбился.

image = [[NSMutableArray alloc] initWithObjects:[UIImage imageNamed:@"[email protected]"], [UIImage imageNamed:@"[email protected]"], nil]; 
name = [[NSMutableArray alloc] initWithObjects:@"Dinner Bell Annouse 50% Discount on Punjabi Food!", nil]; 
time = [[NSMutableArray alloc] initWithObjects:@"13M", nil]; 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return image.count; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    UIImageView *img = (UIImageView *) [cell.contentView viewWithTag:720]; 
    UILabel *lbl1 = (UILabel *) [cell.contentView viewWithTag:721]; 
    UILabel *lbl2 = (UILabel *) [cell.contentView viewWithTag:722]; 
    img.image = [image objectAtIndex:indexPath.row]; 
// lbl1.text=[name objectAtIndex:indexPath.row]; 
// lbl2.text=[time objectAtIndex:indexPath.row]; 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 
    } 
    return cell; 
} 
+0

какая ошибка? –

+0

Не могли бы вы разместить здесь краш-лог? – iPeter

ответ

0

Попробуйте этот код

image=[[NSMutableArray alloc]initWithObjects:[UIImage imageNamed:@"[email protected]"], nil]; 
name=[[NSMutableArray alloc]initWithObjects:@"Dinner Bell Annouse 50% Discount on Punjabi Food!", nil]; 
time=[[NSMutableArray alloc]initWithObjects:@"13M", nil]; 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section 
{ 
return image.count; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
UIImageView *img=(UIImageView*)[cell.contentView viewWithTag:720]; 
UILabel *lbl1=(UILabel *)[cell.contentView viewWithTag:721]; 
UILabel *lbl2=(UILabel *)[cell.contentView viewWithTag:722]; 
img.image=[image objectAtIndex:indexPath.row]; 
// lbl1.text=[name objectAtIndex:indexPath.row]; 
// lbl2.text=[time objectAtIndex:indexPath.row]; 

if (cell == nil) 
{ 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 
} 
    return cell; 
} 
+0

Спасибо. это работает для меня – Ethan

0

Там Количество изображений массива было имя и времени массив 1 объект 2.But только there.So вы должны дать один и тот же размер для трех массивов.

+0

ya Я сделал это, но я взял один NSMutableDictionary и сохранил в нем весь массив .but it crash data = [[NSMutableDictionary alloc] init]; [data setObject: name forKey: @ "name"]; [data setObject: image forKey: @ "image"]; [data setObject: dis forKey: @ "discrip"]; – Ethan

+0

Создайте такой словарь NSDictionary * DIc = [NSDictionary DictionarywithObjectsAndkeys: [UIImage imageNamed: @ "demo_business_image @ .png"], @ "Изображение", @ "Ужин Bell Annouse 50% скидка на пенджабскую еду!", "Имя »@ "13М", @ "Время", ноль]; И хранить объект Dic в nmutable array NSMutableArray * Arr = [NSMutableArray alloc] init]; [Arr addObject: Dic]; –

0
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section 
{return image.count;} 

результат image.count было 2 (значение из массива изображений) от кодирования, когда cellForRowAtIndexPath называется 2 раза для каждого массива , но и в других массивах есть 1 только значение, оно должно показывать индекс ошибки связанного массива

0

В вашем коде количество изображений равно 2, поэтому измените его, так как ячейка будет создана 2 раза в соответствии с номером раздела в виде таблицы, так как имя & массив времени содержит только 1 объект, поэтому добавьте равное количество объектов в обоих NSMutableArray.

Чем вы получите ожидаемый результат. Или вы можете попробовать NSDictionary с парами значений ключей.

Смежные вопросы