簡體   English   中英

iOS TableView在每個單元格中顯示相同的對象

[英]iOS TableView is displaying same object in every cell

我有一個奇怪的問題,我正在設置一個非常基本的表視圖來顯示聯系人,我有tabelviewcontroller,如下所示(我試圖包括所有相關部分),但由於某些原因,當我運行應用程序時,我有相同的數據標簽出現在每個表格單元格中(全部七個)。 有人可以看到錯誤我不能...非常感謝

以下所有代碼都來自tableviewcontroller.m我使我的數組在視圖中加載

- (void)viewDidLoad
{
[super viewDidLoad];

// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;

// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;

self.contacts = [NSMutableArray arrayWithCapacity:10];
Contact *contact = [[Contact alloc] init];
contact.name = @"1";
[self.contacts addObject:contact];
contact.name = @"2";
[self.contacts addObject:contact];
contact.name = @"3";
[self.contacts addObject:contact];
contact.name = @"4";
[self.contacts addObject:contact];
contact.name = @"5";
[self.contacts addObject:contact];
contact.name = @"6";
[self.contacts addObject:contact];
contact.name = @"7";
[self.contacts addObject:contact];
}

我有1節

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}

我有我的數組中的行數

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.contacts count];
}

我在這里創建我的細胞

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ContactTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ContactCell"];
Contact *contact = [self.contacts objectAtIndex:[indexPath row]];
cell.contactNameLabel.text = contact.name;
return cell;
}

所以我不確定為什么每個表格標簽都說“7”,提前謝謝你

viewDidLoad

- (void)viewDidLoad {

     self.contacts = [NSMutableArray arrayWithCapacity:10];
     Contact *contact = [[Contact alloc] init];
     contact.name = @"1";
     [self.contacts addObject:contact];

     //Create new instance of Contact
     contact = [[Contact alloc] init];
     contact.name = @"2";
     [self.contacts addObject:contact];

     //Add objects same in way
}

如果沒有創建新對象[[Contact alloc] init] ,則表示您正在更改同一個對象。

每次將其添加到數據源時,都需要創建一個新的Contact實例。 像這樣

self.contacts = [NSMutableArray arrayWithCapacity:10];
    for(int i=1; i<8;i++) {
        Contact *contact = [[Contact alloc] init];
        contact.name = <some value>;
        [self.contacts addObject:contact];
    }

希望這可以幫助。

它應該是這樣的

First Contact

 Contact *contact = [[Contact alloc] init];
    contact.name = @"1";
    [self.contacts addObject:contact];

第二次接觸

    contact = [[Contact alloc] init];
contact.name = @"2";
[self.contacts addObject:contact];

這是因為您在每個索引中添加相同的對象。 每次將新對象添加到數組之前,都需要初始化它。 請參閱以下示例

Contact *contact1 = [[Contact alloc] init];
contact.name = @"1";
[self.contacts addObject:contact1];
Contact *contact2 = [[Contact alloc] init];
contact.name = @"2";
[self.contacts addObject:contact2];

您需要每次都像下面的代碼一樣創建新對象。

self.contacts = [NSMutableArray arrayWithCapacity:10];
Contact *contact = [[Contact alloc] init];
contact.name = @"1";
[self.contacts addObject:contact];

contact = [[Contact alloc] init];
contact.name = @"2";
[self.contacts addObject:contact];

contact = [[Contact alloc] init];

contact.name = @"3";
[self.contacts addObject:contact];

contact = [[Contact alloc] init];

contact.name = @"4";
[self.contacts addObject:contact];

contact = [[Contact alloc] init];

contact.name = @"5";
[self.contacts addObject:contact];

contact = [[Contact alloc] init];

contact.name = @"6";
[self.contacts addObject:contact];

contact = [[Contact alloc] init];

contact.name = @"7";
[self.contacts addObject:contact];

您正在向數組添加相同的對象,因此基本上數組中的所有7個項目都只是指向同一對象的指針(值為7)。 嘗試為每個號碼創建一個新聯系人,就像這樣

Contact *contact1 = [[Contact alloc] init];
Contact *contact2 = [[Contact alloc] init];
Contact *contact3 = [[Contact alloc] init];

等。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM